SimpleMessageTest.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. <?php
  2. require_once 'Swift/Mime/MimeEntity.php';
  3. require_once 'Swift/Mime/SimpleMessage.php';
  4. require_once 'Swift/Mime/MimePartTest.php';
  5. require_once 'Swift/Mime/Header.php';
  6. require_once 'Swift/Mime/ParameterizedHeader.php';
  7. require_once 'Swift/Mime/Grammar.php';
  8. class Swift_Mime_SimpleMessageTest extends Swift_Mime_MimePartTest
  9. {
  10. public function testNestingLevelIsSubpart()
  11. { //Overridden
  12. }
  13. public function testNestingLevelIsTop()
  14. {
  15. $message = $this->_createMessage($this->_createHeaderSet(),
  16. $this->_createEncoder(), $this->_createCache()
  17. );
  18. $this->assertEqual(
  19. Swift_Mime_MimeEntity::LEVEL_TOP, $message->getNestingLevel()
  20. );
  21. }
  22. public function testDateIsReturnedFromHeader()
  23. {
  24. $date = $this->_createHeader('Date', 123);
  25. $message = $this->_createMessage(
  26. $this->_createHeaderSet(array('Date' => $date)),
  27. $this->_createEncoder(), $this->_createCache()
  28. );
  29. $this->assertEqual(123, $message->getDate());
  30. }
  31. public function testDateIsSetInHeader()
  32. {
  33. $date = $this->_createHeader('Date', 123, array(), false);
  34. $this->_checking(Expectations::create()
  35. -> one($date)->setFieldBodyModel(1234)
  36. -> ignoring($date)
  37. );
  38. $message = $this->_createMessage(
  39. $this->_createHeaderSet(array('Date' => $date)),
  40. $this->_createEncoder(), $this->_createCache()
  41. );
  42. $message->setDate(1234);
  43. }
  44. public function testDateHeaderIsCreatedIfNonePresent()
  45. {
  46. $headers = $this->_createHeaderSet(array(), false);
  47. $this->_checking(Expectations::create()
  48. -> one($headers)->addDateHeader('Date', 1234)
  49. -> ignoring($headers)
  50. );
  51. $message = $this->_createMessage($headers, $this->_createEncoder(),
  52. $this->_createCache()
  53. );
  54. $message->setDate(1234);
  55. }
  56. public function testDateHeaderIsAddedDuringConstruction()
  57. {
  58. $headers = $this->_createHeaderSet(array(), false);
  59. $this->_checking(Expectations::create()
  60. -> one($headers)->addDateHeader('Date', pattern('/^[0-9]+$/D'))
  61. -> ignoring($headers)
  62. );
  63. $message = $this->_createMessage($headers, $this->_createEncoder(),
  64. $this->_createCache()
  65. );
  66. }
  67. public function testIdIsReturnedFromHeader()
  68. {
  69. /* -- RFC 2045, 7.
  70. In constructing a high-level user agent, it may be desirable to allow
  71. one body to make reference to another. Accordingly, bodies may be
  72. labelled using the "Content-ID" header field, which is syntactically
  73. identical to the "Message-ID" header field
  74. */
  75. $messageId = $this->_createHeader('Message-ID', 'a@b');
  76. $message = $this->_createMessage(
  77. $this->_createHeaderSet(array('Message-ID' => $messageId)),
  78. $this->_createEncoder(), $this->_createCache()
  79. );
  80. $this->assertEqual('a@b', $message->getId());
  81. }
  82. public function testIdIsSetInHeader()
  83. {
  84. $messageId = $this->_createHeader('Message-ID', 'a@b', array(), false);
  85. $this->_checking(Expectations::create()
  86. -> one($messageId)->setFieldBodyModel('x@y')
  87. -> ignoring($messageId)
  88. );
  89. $message = $this->_createMessage(
  90. $this->_createHeaderSet(array('Message-ID' => $messageId)),
  91. $this->_createEncoder(), $this->_createCache()
  92. );
  93. $message->setId('x@y');
  94. }
  95. public function testIdIsAutoGenerated()
  96. {
  97. $headers = $this->_createHeaderSet(array(), false);
  98. $this->_checking(Expectations::create()
  99. -> one($headers)->addIdHeader('Message-ID', pattern('/^.*?@.*?$/D'))
  100. -> ignoring($headers)
  101. );
  102. $message = $this->_createMessage($headers, $this->_createEncoder(),
  103. $this->_createCache()
  104. );
  105. }
  106. public function testSubjectIsReturnedFromHeader()
  107. {
  108. /* -- RFC 2822, 3.6.5.
  109. */
  110. $subject = $this->_createHeader('Subject', 'example subject');
  111. $message = $this->_createMessage(
  112. $this->_createHeaderSet(array('Subject' => $subject)),
  113. $this->_createEncoder(), $this->_createCache()
  114. );
  115. $this->assertEqual('example subject', $message->getSubject());
  116. }
  117. public function testSubjectIsSetInHeader()
  118. {
  119. $subject = $this->_createHeader('Subject', '', array(), false);
  120. $this->_checking(Expectations::create()
  121. -> one($subject)->setFieldBodyModel('foo')
  122. -> ignoring($subject)
  123. );
  124. $message = $this->_createMessage(
  125. $this->_createHeaderSet(array('Subject' => $subject)),
  126. $this->_createEncoder(), $this->_createCache()
  127. );
  128. $message->setSubject('foo');
  129. }
  130. public function testSubjectHeaderIsCreatedIfNotPresent()
  131. {
  132. $headers = $this->_createHeaderSet(array(), false);
  133. $this->_checking(Expectations::create()
  134. -> one($headers)->addTextHeader('Subject', 'example subject')
  135. -> ignoring($headers)
  136. );
  137. $message = $this->_createMessage($headers, $this->_createEncoder(),
  138. $this->_createCache()
  139. );
  140. $message->setSubject('example subject');
  141. }
  142. public function testReturnPathIsReturnedFromHeader()
  143. {
  144. /* -- RFC 2822, 3.6.7.
  145. */
  146. $path = $this->_createHeader('Return-Path', 'bounces@domain');
  147. $message = $this->_createMessage(
  148. $this->_createHeaderSet(array('Return-Path' => $path)),
  149. $this->_createEncoder(), $this->_createCache()
  150. );
  151. $this->assertEqual('bounces@domain', $message->getReturnPath());
  152. }
  153. public function testReturnPathIsSetInHeader()
  154. {
  155. $path = $this->_createHeader('Return-Path', '', array(), false);
  156. $this->_checking(Expectations::create()
  157. -> one($path)->setFieldBodyModel('bounces@domain')
  158. -> ignoring($path)
  159. );
  160. $message = $this->_createMessage(
  161. $this->_createHeaderSet(array('Return-Path' => $path)),
  162. $this->_createEncoder(), $this->_createCache()
  163. );
  164. $message->setReturnPath('bounces@domain');
  165. }
  166. public function testReturnPathHeaderIsAddedIfNoneSet()
  167. {
  168. $headers = $this->_createHeaderSet(array(), false);
  169. $this->_checking(Expectations::create()
  170. -> one($headers)->addPathHeader('Return-Path', 'bounces@domain')
  171. -> ignoring($headers)
  172. );
  173. $message = $this->_createMessage($headers, $this->_createEncoder(),
  174. $this->_createCache()
  175. );
  176. $message->setReturnPath('bounces@domain');
  177. }
  178. public function testSenderIsReturnedFromHeader()
  179. {
  180. /* -- RFC 2822, 3.6.2.
  181. */
  182. $sender = $this->_createHeader('Sender', array('sender@domain'=>'Name'));
  183. $message = $this->_createMessage(
  184. $this->_createHeaderSet(array('Sender' => $sender)),
  185. $this->_createEncoder(), $this->_createCache()
  186. );
  187. $this->assertEqual(array('sender@domain'=>'Name'), $message->getSender());
  188. }
  189. public function testSenderIsSetInHeader()
  190. {
  191. $sender = $this->_createHeader('Sender', array('sender@domain'=>'Name'),
  192. array(), false
  193. );
  194. $this->_checking(Expectations::create()
  195. -> one($sender)->setFieldBodyModel(array('other@domain'=>'Other'))
  196. -> ignoring($sender)
  197. );
  198. $message = $this->_createMessage(
  199. $this->_createHeaderSet(array('Sender' => $sender)),
  200. $this->_createEncoder(), $this->_createCache()
  201. );
  202. $message->setSender(array('other@domain'=>'Other'));
  203. }
  204. public function testSenderHeaderIsAddedIfNoneSet()
  205. {
  206. $headers = $this->_createHeaderSet(array(), false);
  207. $this->_checking(Expectations::create()
  208. -> one($headers)->addMailboxHeader('Sender', (array) 'sender@domain')
  209. -> ignoring($headers)
  210. );
  211. $message = $this->_createMessage($headers, $this->_createEncoder(),
  212. $this->_createCache()
  213. );
  214. $message->setSender('sender@domain');
  215. }
  216. public function testNameCanBeUsedInSenderHeader()
  217. {
  218. $headers = $this->_createHeaderSet(array(), false);
  219. $this->_checking(Expectations::create()
  220. -> one($headers)->addMailboxHeader('Sender', array('sender@domain'=>'Name'))
  221. -> ignoring($headers)
  222. );
  223. $message = $this->_createMessage($headers, $this->_createEncoder(),
  224. $this->_createCache()
  225. );
  226. $message->setSender('sender@domain', 'Name');
  227. }
  228. public function testFromIsReturnedFromHeader()
  229. {
  230. /* -- RFC 2822, 3.6.2.
  231. */
  232. $from = $this->_createHeader('From', array('from@domain'=>'Name'));
  233. $message = $this->_createMessage(
  234. $this->_createHeaderSet(array('From' => $from)),
  235. $this->_createEncoder(), $this->_createCache()
  236. );
  237. $this->assertEqual(array('from@domain'=>'Name'), $message->getFrom());
  238. }
  239. public function testFromIsSetInHeader()
  240. {
  241. $from = $this->_createHeader('From', array('from@domain'=>'Name'),
  242. array(), false
  243. );
  244. $this->_checking(Expectations::create()
  245. -> one($from)->setFieldBodyModel(array('other@domain'=>'Other'))
  246. -> ignoring($from)
  247. );
  248. $message = $this->_createMessage(
  249. $this->_createHeaderSet(array('From' => $from)),
  250. $this->_createEncoder(), $this->_createCache()
  251. );
  252. $message->setFrom(array('other@domain'=>'Other'));
  253. }
  254. public function testFromIsAddedToHeadersDuringAddFrom()
  255. {
  256. $from = $this->_createHeader('From', array('from@domain'=>'Name'),
  257. array(), false
  258. );
  259. $this->_checking(Expectations::create()
  260. -> one($from)->setFieldBodyModel(array('from@domain'=>'Name', 'other@domain'=>'Other'))
  261. -> ignoring($from)
  262. );
  263. $message = $this->_createMessage(
  264. $this->_createHeaderSet(array('From' => $from)),
  265. $this->_createEncoder(), $this->_createCache()
  266. );
  267. $message->addFrom('other@domain', 'Other');
  268. }
  269. public function testFromHeaderIsAddedIfNoneSet()
  270. {
  271. $headers = $this->_createHeaderSet(array(), false);
  272. $this->_checking(Expectations::create()
  273. -> one($headers)->addMailboxHeader('From', (array) 'from@domain')
  274. -> ignoring($headers)
  275. );
  276. $message = $this->_createMessage($headers, $this->_createEncoder(),
  277. $this->_createCache()
  278. );
  279. $message->setFrom('from@domain');
  280. }
  281. public function testPersonalNameCanBeUsedInFromAddress()
  282. {
  283. $headers = $this->_createHeaderSet(array(), false);
  284. $this->_checking(Expectations::create()
  285. -> one($headers)->addMailboxHeader('From', array('from@domain'=>'Name'))
  286. -> ignoring($headers)
  287. );
  288. $message = $this->_createMessage($headers, $this->_createEncoder(),
  289. $this->_createCache()
  290. );
  291. $message->setFrom('from@domain', 'Name');
  292. }
  293. public function testReplyToIsReturnedFromHeader()
  294. {
  295. /* -- RFC 2822, 3.6.2.
  296. */
  297. $reply = $this->_createHeader('Reply-To', array('reply@domain'=>'Name'));
  298. $message = $this->_createMessage(
  299. $this->_createHeaderSet(array('Reply-To' => $reply)),
  300. $this->_createEncoder(), $this->_createCache()
  301. );
  302. $this->assertEqual(array('reply@domain'=>'Name'), $message->getReplyTo());
  303. }
  304. public function testReplyToIsSetInHeader()
  305. {
  306. $reply = $this->_createHeader('Reply-To', array('reply@domain'=>'Name'),
  307. array(), false
  308. );
  309. $this->_checking(Expectations::create()
  310. -> one($reply)->setFieldBodyModel(array('other@domain'=>'Other'))
  311. -> ignoring($reply)
  312. );
  313. $message = $this->_createMessage(
  314. $this->_createHeaderSet(array('Reply-To' => $reply)),
  315. $this->_createEncoder(), $this->_createCache()
  316. );
  317. $message->setReplyTo(array('other@domain'=>'Other'));
  318. }
  319. public function testReplyToIsAddedToHeadersDuringAddReplyTo()
  320. {
  321. $replyTo = $this->_createHeader('Reply-To', array('from@domain'=>'Name'),
  322. array(), false
  323. );
  324. $this->_checking(Expectations::create()
  325. -> one($replyTo)->setFieldBodyModel(array('from@domain'=>'Name', 'other@domain'=>'Other'))
  326. -> ignoring($replyTo)
  327. );
  328. $message = $this->_createMessage(
  329. $this->_createHeaderSet(array('Reply-To' => $replyTo)),
  330. $this->_createEncoder(), $this->_createCache()
  331. );
  332. $message->addReplyTo('other@domain', 'Other');
  333. }
  334. public function testReplyToHeaderIsAddedIfNoneSet()
  335. {
  336. $headers = $this->_createHeaderSet(array(), false);
  337. $this->_checking(Expectations::create()
  338. -> one($headers)->addMailboxHeader('Reply-To', (array) 'reply@domain')
  339. -> ignoring($headers)
  340. );
  341. $message = $this->_createMessage($headers, $this->_createEncoder(),
  342. $this->_createCache()
  343. );
  344. $message->setReplyTo('reply@domain');
  345. }
  346. public function testNameCanBeUsedInReplyTo()
  347. {
  348. $headers = $this->_createHeaderSet(array(), false);
  349. $this->_checking(Expectations::create()
  350. -> one($headers)->addMailboxHeader('Reply-To', array('reply@domain'=>'Name'))
  351. -> ignoring($headers)
  352. );
  353. $message = $this->_createMessage($headers, $this->_createEncoder(),
  354. $this->_createCache()
  355. );
  356. $message->setReplyTo('reply@domain', 'Name');
  357. }
  358. public function testToIsReturnedFromHeader()
  359. {
  360. /* -- RFC 2822, 3.6.3.
  361. */
  362. $to = $this->_createHeader('To', array('to@domain'=>'Name'));
  363. $message = $this->_createMessage(
  364. $this->_createHeaderSet(array('To' => $to)),
  365. $this->_createEncoder(), $this->_createCache()
  366. );
  367. $this->assertEqual(array('to@domain'=>'Name'), $message->getTo());
  368. }
  369. public function testToIsSetInHeader()
  370. {
  371. $to = $this->_createHeader('To', array('to@domain'=>'Name'),
  372. array(), false
  373. );
  374. $this->_checking(Expectations::create()
  375. -> one($to)->setFieldBodyModel(array('other@domain'=>'Other'))
  376. -> ignoring($to)
  377. );
  378. $message = $this->_createMessage(
  379. $this->_createHeaderSet(array('To' => $to)),
  380. $this->_createEncoder(), $this->_createCache()
  381. );
  382. $message->setTo(array('other@domain'=>'Other'));
  383. }
  384. public function testToIsAddedToHeadersDuringAddTo()
  385. {
  386. $to = $this->_createHeader('To', array('from@domain'=>'Name'),
  387. array(), false
  388. );
  389. $this->_checking(Expectations::create()
  390. -> one($to)->setFieldBodyModel(array('from@domain'=>'Name', 'other@domain'=>'Other'))
  391. -> ignoring($to)
  392. );
  393. $message = $this->_createMessage(
  394. $this->_createHeaderSet(array('To' => $to)),
  395. $this->_createEncoder(), $this->_createCache()
  396. );
  397. $message->addTo('other@domain', 'Other');
  398. }
  399. public function testToHeaderIsAddedIfNoneSet()
  400. {
  401. $headers = $this->_createHeaderSet(array(), false);
  402. $this->_checking(Expectations::create()
  403. -> one($headers)->addMailboxHeader('To', (array) 'to@domain')
  404. -> ignoring($headers)
  405. );
  406. $message = $this->_createMessage($headers, $this->_createEncoder(),
  407. $this->_createCache()
  408. );
  409. $message->setTo('to@domain');
  410. }
  411. public function testNameCanBeUsedInToHeader()
  412. {
  413. $headers = $this->_createHeaderSet(array(), false);
  414. $this->_checking(Expectations::create()
  415. -> one($headers)->addMailboxHeader('To', array('to@domain'=>'Name'))
  416. -> ignoring($headers)
  417. );
  418. $message = $this->_createMessage($headers, $this->_createEncoder(),
  419. $this->_createCache()
  420. );
  421. $message->setTo('to@domain', 'Name');
  422. }
  423. public function testCcIsReturnedFromHeader()
  424. {
  425. /* -- RFC 2822, 3.6.3.
  426. */
  427. $cc = $this->_createHeader('Cc', array('cc@domain'=>'Name'));
  428. $message = $this->_createMessage(
  429. $this->_createHeaderSet(array('Cc' => $cc)),
  430. $this->_createEncoder(), $this->_createCache()
  431. );
  432. $this->assertEqual(array('cc@domain'=>'Name'), $message->getCc());
  433. }
  434. public function testCcIsSetInHeader()
  435. {
  436. $cc = $this->_createHeader('Cc', array('cc@domain'=>'Name'),
  437. array(), false
  438. );
  439. $this->_checking(Expectations::create()
  440. -> one($cc)->setFieldBodyModel(array('other@domain'=>'Other'))
  441. -> ignoring($cc)
  442. );
  443. $message = $this->_createMessage(
  444. $this->_createHeaderSet(array('Cc' => $cc)),
  445. $this->_createEncoder(), $this->_createCache()
  446. );
  447. $message->setCc(array('other@domain'=>'Other'));
  448. }
  449. public function testCcIsAddedToHeadersDuringAddCc()
  450. {
  451. $cc = $this->_createHeader('Cc', array('from@domain'=>'Name'),
  452. array(), false
  453. );
  454. $this->_checking(Expectations::create()
  455. -> one($cc)->setFieldBodyModel(array('from@domain'=>'Name', 'other@domain'=>'Other'))
  456. -> ignoring($cc)
  457. );
  458. $message = $this->_createMessage(
  459. $this->_createHeaderSet(array('Cc' => $cc)),
  460. $this->_createEncoder(), $this->_createCache()
  461. );
  462. $message->addCc('other@domain', 'Other');
  463. }
  464. public function testCcHeaderIsAddedIfNoneSet()
  465. {
  466. $headers = $this->_createHeaderSet(array(), false);
  467. $this->_checking(Expectations::create()
  468. -> one($headers)->addMailboxHeader('Cc', (array) 'cc@domain')
  469. -> ignoring($headers)
  470. );
  471. $message = $this->_createMessage($headers, $this->_createEncoder(),
  472. $this->_createCache()
  473. );
  474. $message->setCc('cc@domain');
  475. }
  476. public function testNameCanBeUsedInCcHeader()
  477. {
  478. $headers = $this->_createHeaderSet(array(), false);
  479. $this->_checking(Expectations::create()
  480. -> one($headers)->addMailboxHeader('Cc', array('cc@domain'=>'Name'))
  481. -> ignoring($headers)
  482. );
  483. $message = $this->_createMessage($headers, $this->_createEncoder(),
  484. $this->_createCache()
  485. );
  486. $message->setCc('cc@domain', 'Name');
  487. }
  488. public function testBccIsReturnedFromHeader()
  489. {
  490. /* -- RFC 2822, 3.6.3.
  491. */
  492. $bcc = $this->_createHeader('Bcc', array('bcc@domain'=>'Name'));
  493. $message = $this->_createMessage(
  494. $this->_createHeaderSet(array('Bcc' => $bcc)),
  495. $this->_createEncoder(), $this->_createCache()
  496. );
  497. $this->assertEqual(array('bcc@domain'=>'Name'), $message->getBcc());
  498. }
  499. public function testBccIsSetInHeader()
  500. {
  501. $bcc = $this->_createHeader('Bcc', array('bcc@domain'=>'Name'),
  502. array(), false
  503. );
  504. $this->_checking(Expectations::create()
  505. -> one($bcc)->setFieldBodyModel(array('other@domain'=>'Other'))
  506. -> ignoring($bcc)
  507. );
  508. $message = $this->_createMessage(
  509. $this->_createHeaderSet(array('Bcc' => $bcc)),
  510. $this->_createEncoder(), $this->_createCache()
  511. );
  512. $message->setBcc(array('other@domain'=>'Other'));
  513. }
  514. public function testBccIsAddedToHeadersDuringAddBcc()
  515. {
  516. $bcc = $this->_createHeader('Bcc', array('from@domain'=>'Name'),
  517. array(), false
  518. );
  519. $this->_checking(Expectations::create()
  520. -> one($bcc)->setFieldBodyModel(array('from@domain'=>'Name', 'other@domain'=>'Other'))
  521. -> ignoring($bcc)
  522. );
  523. $message = $this->_createMessage(
  524. $this->_createHeaderSet(array('Bcc' => $bcc)),
  525. $this->_createEncoder(), $this->_createCache()
  526. );
  527. $message->addBcc('other@domain', 'Other');
  528. }
  529. public function testBccHeaderIsAddedIfNoneSet()
  530. {
  531. $headers = $this->_createHeaderSet(array(), false);
  532. $this->_checking(Expectations::create()
  533. -> one($headers)->addMailboxHeader('Bcc', (array) 'bcc@domain')
  534. -> ignoring($headers)
  535. );
  536. $message = $this->_createMessage($headers, $this->_createEncoder(),
  537. $this->_createCache()
  538. );
  539. $message->setBcc('bcc@domain');
  540. }
  541. public function testNameCanBeUsedInBcc()
  542. {
  543. $headers = $this->_createHeaderSet(array(), false);
  544. $this->_checking(Expectations::create()
  545. -> one($headers)->addMailboxHeader('Bcc', array('bcc@domain'=>'Name'))
  546. -> ignoring($headers)
  547. );
  548. $message = $this->_createMessage($headers, $this->_createEncoder(),
  549. $this->_createCache()
  550. );
  551. $message->setBcc('bcc@domain', 'Name');
  552. }
  553. public function testPriorityIsReadFromHeader()
  554. {
  555. $prio = $this->_createHeader('X-Priority', '2 (High)');
  556. $message = $this->_createMessage(
  557. $this->_createHeaderSet(array('X-Priority' => $prio)),
  558. $this->_createEncoder(), $this->_createCache()
  559. );
  560. $this->assertEqual(2, $message->getPriority());
  561. }
  562. public function testPriorityIsSetInHeader()
  563. {
  564. $prio = $this->_createHeader('X-Priority', '2 (High)', array(), false);
  565. $this->_checking(Expectations::create()
  566. -> one($prio)->setFieldBodyModel('5 (Lowest)')
  567. -> ignoring($prio)
  568. );
  569. $message = $this->_createMessage(
  570. $this->_createHeaderSet(array('X-Priority' => $prio)),
  571. $this->_createEncoder(), $this->_createCache()
  572. );
  573. $message->setPriority(5);
  574. }
  575. public function testPriorityHeaderIsAddedIfNoneSet()
  576. {
  577. $headers = $this->_createHeaderSet(array(), false);
  578. $this->_checking(Expectations::create()
  579. -> one($headers)->addTextHeader('X-Priority', '4 (Low)')
  580. -> ignoring($headers)
  581. );
  582. $message = $this->_createMessage($headers, $this->_createEncoder(),
  583. $this->_createCache()
  584. );
  585. $message->setPriority(4);
  586. }
  587. public function testReadReceiptAddressReadFromHeader()
  588. {
  589. $rcpt = $this->_createHeader('Disposition-Notification-To',
  590. array('chris@swiftmailer.org'=>'Chris')
  591. );
  592. $message = $this->_createMessage(
  593. $this->_createHeaderSet(array('Disposition-Notification-To' => $rcpt)),
  594. $this->_createEncoder(), $this->_createCache()
  595. );
  596. $this->assertEqual(array('chris@swiftmailer.org'=>'Chris'),
  597. $message->getReadReceiptTo()
  598. );
  599. }
  600. public function testReadReceiptIsSetInHeader()
  601. {
  602. $rcpt = $this->_createHeader('Disposition-Notification-To', array(), array(), false);
  603. $this->_checking(Expectations::create()
  604. -> one($rcpt)->setFieldBodyModel('mark@swiftmailer.org')
  605. -> ignoring($rcpt)
  606. );
  607. $message = $this->_createMessage(
  608. $this->_createHeaderSet(array('Disposition-Notification-To' => $rcpt)),
  609. $this->_createEncoder(), $this->_createCache()
  610. );
  611. $message->setReadReceiptTo('mark@swiftmailer.org');
  612. }
  613. public function testReadReceiptHeaderIsAddedIfNoneSet()
  614. {
  615. $headers = $this->_createHeaderSet(array(), false);
  616. $this->_checking(Expectations::create()
  617. -> one($headers)->addMailboxHeader(
  618. 'Disposition-Notification-To', 'mark@swiftmailer.org'
  619. )
  620. -> ignoring($headers)
  621. );
  622. $message = $this->_createMessage($headers, $this->_createEncoder(),
  623. $this->_createCache()
  624. );
  625. $message->setReadReceiptTo('mark@swiftmailer.org');
  626. }
  627. public function testChildrenCanBeAttached()
  628. {
  629. $child1 = $this->_createChild();
  630. $child2 = $this->_createChild();
  631. $message = $this->_createMessage($this->_createHeaderSet(),
  632. $this->_createEncoder(), $this->_createCache()
  633. );
  634. $message->attach($child1);
  635. $message->attach($child2);
  636. $this->assertEqual(array($child1, $child2), $message->getChildren());
  637. }
  638. public function testChildrenCanBeDetached()
  639. {
  640. $child1 = $this->_createChild();
  641. $child2 = $this->_createChild();
  642. $message = $this->_createMessage($this->_createHeaderSet(),
  643. $this->_createEncoder(), $this->_createCache()
  644. );
  645. $message->attach($child1);
  646. $message->attach($child2);
  647. $message->detach($child1);
  648. $this->assertEqual(array($child2), $message->getChildren());
  649. }
  650. public function testEmbedAttachesChild()
  651. {
  652. $child = $this->_createChild();
  653. $message = $this->_createMessage($this->_createHeaderSet(),
  654. $this->_createEncoder(), $this->_createCache()
  655. );
  656. $message->embed($child);
  657. $this->assertEqual(array($child), $message->getChildren());
  658. }
  659. public function testEmbedReturnsValidCid()
  660. {
  661. $child = $this->_createChild(Swift_Mime_MimeEntity::LEVEL_RELATED, '',
  662. false
  663. );
  664. $this->_checking(Expectations::create()
  665. -> ignoring($child)->getId() -> returns('foo@bar')
  666. -> ignoring($child)
  667. );
  668. $message = $this->_createMessage($this->_createHeaderSet(),
  669. $this->_createEncoder(), $this->_createCache()
  670. );
  671. $this->assertEqual('cid:foo@bar', $message->embed($child));
  672. }
  673. public function testFluidInterface()
  674. {
  675. $child = $this->_createChild();
  676. $message = $this->_createMessage($this->_createHeaderSet(),
  677. $this->_createEncoder(), $this->_createCache()
  678. );
  679. $this->assertSame($message,
  680. $message
  681. ->setContentType('text/plain')
  682. ->setEncoder($this->_createEncoder())
  683. ->setId('foo@bar')
  684. ->setDescription('my description')
  685. ->setMaxLineLength(998)
  686. ->setBody('xx')
  687. ->setBoundary('xyz')
  688. ->setChildren(array())
  689. ->setCharset('iso-8859-1')
  690. ->setFormat('flowed')
  691. ->setDelSp(false)
  692. ->setSubject('subj')
  693. ->setDate(123)
  694. ->setReturnPath('foo@bar')
  695. ->setSender('foo@bar')
  696. ->setFrom(array('x@y' => 'XY'))
  697. ->setReplyTo(array('ab@cd' => 'ABCD'))
  698. ->setTo(array('chris@site.tld', 'mark@site.tld'))
  699. ->setCc('john@somewhere.tld')
  700. ->setBcc(array('one@site', 'two@site' => 'Two'))
  701. ->setPriority(4)
  702. ->setReadReceiptTo('a@b')
  703. ->attach($child)
  704. ->detach($child)
  705. );
  706. }
  707. // -- Private helpers
  708. //abstract
  709. protected function _createEntity($headers, $encoder, $cache)
  710. {
  711. return $this->_createMessage($headers, $encoder, $cache);
  712. }
  713. protected function _createMimePart($headers, $encoder, $cache)
  714. {
  715. return $this->_createMessage($headers, $encoder, $cache);
  716. }
  717. private function _createMessage($headers, $encoder, $cache)
  718. {
  719. return new Swift_Mime_SimpleMessage($headers, $encoder, $cache, new Swift_Mime_Grammar());
  720. }
  721. }