How could I have had such spiritual experiences when the doubts I am having are all related to the foundation of the church?
I have a theory on that. I too felt the overwhelming spirituality when I prayed about whether the church is true. But there came a point in my life when the church stopped "working" for me.
As I learned more and more about the anomalies and experienced leadership abuse things changed. I found that in spite of the myths I heard at church -- that no one is paid, when GA's are in fact paid. That Mountain Meadows Massacre did occur, that JS had many wives that were married to other men, Fannie Alger, the Priesthood ban repudiation, etcetera, I found it hard to reconcile these facts with my testimony.
I now believe that at that point in my life -- when I got my testimony -- the LDS church was the best place I could be. I needed, and wanted the structure, I wanted the service, and admired the organization and emphasis on execution in the church, and I loved the Book of Mormon and concept of a pre-mortal life. I had engaged with it, and saw no other alternatives that were attractive to me. It would produce net good in my life (and did for many years), so after repeated prayers about it, God said "OK". The net good it would create for me was positive, and there were no other alternatives in which I was interested, so he allowed me to do it and gave me the testimony I wanted. I also believe that after the discussions, I would probably not have committed to any other religion, as even today, they all seem inferior to me.
If I can draw an analogy. I want to found a non-profit that improves a community I am part of. There is an existing group of people who work hard for the community, and have a lot of talent and relationships, as well as achievements in community improvement. I have joined them and gained some credibility with them, and influence. However, the person most likely to be president of this non-profit from this group is a bit of a controversial figure in the community. A lot of people see her as divisive, and she has, in fact, created disunity and polarization among a fairly large group of people through some of her behavior.
In my view, she is not the "perfect face" to head up this non-profit. At the same time, she has A LOT of influence with local business people, and many of the HOA presidents. Also, she knows me, I know her, and she and her friends WANT to get more formal in their organization. They also have a large social media reach they have built up over the last few years -- it is quite an accomplishment.
To find a different set of people to help me found this non-profit would be a lot of work and I had to do that, I would probably not pursue the idea of a formal non-profit. I think it's impractical because success with people in business and non-profit contexts depends on relationships. relationships, relationships, relationships. I would rather have a different president, but taken with the difficulty of finding a different group of people with the social media reach, and desire to found the non-profit, I decided to work with her and propose, to this group, to formalize themselves into a legal non-profit. So, in spite of the weaknesses of this lady, there is net good, and so I think I will help them found this non-profit with her as president of the board of directors.
I wonder if God looked at my question about Mormonism the same way. He knew it wasn't perfect for me-- and maybe not for my whole life. But at that time, it was good for me and had enough positives that he would give me a testimony in spite of the warts the church sports. I was connected to a Mormon who got me the discussions. attended church, admired the church, and even wanted to serve a mission before I even had a testimony (in concept). So, God saw the pieces were in place, that other churches were not palatable to me, so he looked at the net good, and said "Sure -- go for it -- this will bless your life for a long time, and is the best alternative for you right now, in spite of its weaknesses".
I also think God cares less about the truth than we think. I think he cares more about the impact your beliefs have on your personal growth and character, and if that means encouraging you to believe something that is for your good, he will do it through the HOly Ghost, even if you end up believing things that are false, and perhaps even in the long run, are inconsequential when compared to the net good those beliefs bring to your life. Half of what we tell ourselves are falsehoods anyway -- to preserve our inner peace.