t/d/n become flaps when they are (1) after a vowel or glide (including r but not l), (2) before a vowel, and (3) at the end of a syllable. (Condition (3) is stated with the assumption that an intervocalic consonant before an unstressed vowel goes at the end of the preceding syllable.)
"Entertainment" has a rather involved derivation. After nasalizing the preceding vowel, the first n is lost by a rule that deletes nasal consonants before voiceless consonants at the same place of articulation. The first t is now between vowels and at the end of a syllable, so it flaps. The flap assimilates in voice to the preceding and following voiced vowels, and being a sonorant consonant, it also assimilates in nasality to the preceding nasal vowel, so we wind up with a nasal voiced flap between the first two vowels of "entertainment". Nothing remarkable happens in the "tainment" part of the word, except [n] optionally assimilates in position to following [m].
This follows the phonological treatment worked out a long time ago by my teacher David Stampe. Note that there is never a d at any stage of the derivation, and there is never an intervocalic n, either.