I don't think you need to take a test to get promoted specifically to commander. Pulaski held the rank but definitely wasn't interested in command.
I suspect that Troi's taking of the test and her subsequent promotion aren't directly related. She may have been a few "points" shy of eligibility for promotion to commander rank, and by taking the test she earned the promotion. She might have earned the same promotion by merely serving another year or two without ending up qualified for command.
As for Data's rank, he literally lacked ambition, and Starfleet Command was probably reluctant to promote Data any further. I don't think he stalled out at lieutenant commander just because he didn't take a quick test. Data demonstrated aptitude for command when it fell to him, and he was a line officer, but did he possess the other qualities needed for higher rank and position? Starfleet brass probably didn't think so.