The crucible is for silver,
and the furnace is for gold,
and the LORD tests hearts
The extreme heat of the fire purifies precious metals, so also the Lord purifies the hearts of those who are His. Considering the context of the rest of the scriptures, this verse does not seem to be saying that He tests a heart as if He does not know what is in the heart to begin with. One example of such is seen in John 2:25 where it is made clear that the Lord does not need the testimony of men, for He knows what is in man. He who sees all need not be informed as to what is in the hearts of men.
The Lord knows us intimately, yet kindly He does not leave us the way we are currently. One of the reasons He orchestrates certain events and allows particular circumstances is to purify the hearts of those who are His. He has purposes in pain - though often we may not know what these purposes are this side of eternity. His all-knowing intentionality and complete sovereignty are massive foundations for us to rest assured that our sufferings are not in vain.