Why did Allah, may He be glorified and exalted, test Ayyoob (peace be upon him)?
Question

Why did Allah, may He be glorified and exalted, test Ayyoob (peace be upon him)? I heard that one of the vulnerable people asked him for protection and he did not give it to him, so Allah, may He be glorified and exalted, punished him for that. Is this story true?
Praise be to Allah.Firstly: 
The apparent meaning of the Holy Qur’an indicates that Allah’s trial of His Prophet Ayyoob (peace be upon him) was not by way of punishment for a sin or act of disobedience; rather it was for reasons known to Him, may He be glorified and exalted. Perhaps one of those reasons is that it was in order to raise him to high status by virtue of his patience, so that he might attain thereby a lofty status on the Day of Judgement.
Allah, may He be glorified and exalted, praises him for his patience, as He says (interpretation of the meaning): “Truly! We found him patient. How excellent (a) slave! Verily, he was ever oft-returning in repentance (to Us)” [Saad 38:44]. This is the context of praise and commendation, and raising in status, which is different from the context of rebuke that is mentioned in the story of Yoonus (peace be upon him), where Allah, may He be exalted, says (interpretation of the meaning):
“Then a (big) fish swallowed him and he had done an act worthy of blame.
Had he not been of them who glorify Allah,
He would have indeed remained inside its belly (the fish) till the Day of Resurrection.
But We cast him forth on the naked shore while he was sick”
[as-Saaffaat 37:142-145]. 
Secondly: 
In the Prophet’s Sunnah there is a report which indicates that Ayyoob (peace be upon him) was innocent of any sin that could have been a cause for the sickness that befell him. 
It was narrated from Anas ibn Maalik (may Allah be pleased with him) that the Messenger of Allah (blessings and peace of Allah be upon him) said:
“Verily Ayyoob, the Prophet of Allah, suffered his trials for eighteen years, when he was shunned by relatives and strangers alike, except for two of his brothers who were among the closest of his brothers. They used to come to him in the morning and in the evening. One of them said to his companion: Do you know, by Allah, that Ayyoob must have committed a sin that no one else ever committed? His companion said: Why is that? He said: For eighteen years, Allah has not shown mercy to him or granted him relief. 
When they went to him in the evening, the man could not keep from telling him about that. Ayyoob said: I do not know what he is talking about, but Allah knows that I passed by two men who were arguing, and they mentioned Allah, I would go back to my house and offer expiation on their behalf, because I did not want Allah to be mentioned except in a proper manner… 
Narrated by Abu Ya‘la in al-Musnad, 6/299; Ibn Hibbaan in his Saheeh, 7/159; al-Haakim in al-Mustadrak, 2/635 
This hadeeth was classed as saheeh by Ibn Hibbaan. Al-Haakim said: (It is saheeh) according to the conditions of al-Bukhaari and Muslim, although they did not narrate it. This was also stated by adh-Dhahabi in at-Talkhees. In Fath al-Baari, 6/421, Ibn Hajar described it as being the soundest report on this topic. It was classed as saheeh by al-Albaani in as-Silsilah as-Saheehah, no 17. Some of the scholars thought it was flawed. See: Ahaadeeth Mu‘allah Zaahiruha as-Sihhah, p. 54. See also: al-Bidaayah wa’n-Nihaayah, 1/254-259 
Thirdly: 
The scholars stated that the wisdom behind the trials of the Prophets is to raise them in status and enhance their image and reputation. 
Shaykh Ibn Baaz (may Allah have mercy on him) said: 
Allah, may He be glorified and exalted, tests His slaves with good times and bad, hardship and ease. He may test them thereby to raise them in status and enhance their image and reputation, and multiply the reward for their good deeds, as He did with the Prophets and Messengers (blessings and peace of Allah be upon them) and the righteous among His slaves. The Prophet (blessings and peace of Allah be upon him) said: “The most sorely tested of people are the Prophets, then the next best and the next best.” Narrated by at-Tirmidhi, 2398. So if any of the righteous slaves of Allah is tested with sickness and the like, this is akin to the tests of the Prophets and Messengers, and serves to raise one in status and increase the reward, so let him be an example to others of patience and seeking reward with Allah.
End quote from Majmoo‘ Fataawa Ibn Baaz, 4/370-371 
On our website we have previously discussed in detail the various reasons behind the trials of the Prophets, and we have quoted the explanation of this issue given by al-‘Allaamah Ibn al-Qayyim. Please see fatwa no. 72265
And Allah knows best.