PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/x2engine/protected/tests/unit/models/TrackEmailTest.php

https://gitlab.com/e0/X2CRM
PHP | 273 lines | 197 code | 34 blank | 42 comment | 0 complexity | 81f0dd470438e50e752bed05ba45c534 MD5 | raw file
  1. <?php
  2. /***********************************************************************************
  3. * X2CRM is a customer relationship management program developed by
  4. * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
  24. * California 95067, USA. on our website at www.x2crm.com, or at our
  25. * email address: contact@x2engine.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * X2Engine" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by X2Engine".
  36. **********************************************************************************/
  37. Yii::import('application.modules.actions.models.*');
  38. Yii::import('application.modules.contacts.models.*');
  39. Yii::import('application.modules.accounts.models.*');
  40. Yii::import('application.modules.docs.models.*');
  41. Yii::import('application.modules.quotes.models.*');
  42. /**
  43. * Tests on 3 different model classes to verify that a selection of them work.
  44. */
  45. class TrackEmailTest extends X2DbTestCase {
  46. public static function referenceFixtures() {
  47. return array(
  48. 'quote' => 'Quote',
  49. 'contacts' => 'Contacts',
  50. 'accounts' => 'Accounts',
  51. );
  52. }
  53. public $fixtures = array(
  54. 'actions' => 'Actions',
  55. 'actionText' => 'ActionText',
  56. 'trackEmail' => 'TrackEmail',
  57. 'events' => 'Events',
  58. );
  59. public function testContactEmailOpen(){
  60. $contact = $this->contacts('testAnyone');
  61. $this->assertNullEmailOpenAction($contact);
  62. $contactInitialActionCount = Yii::app()->db->createCommand()
  63. ->select('COUNT(*)')
  64. ->from('x2_actions')
  65. ->where('associationType = :type AND associationId = :id',
  66. array(
  67. ':type' => $contact->module,
  68. ':id' => $contact->id,
  69. ))
  70. ->queryScalar();
  71. $action = new Actions();
  72. $action->completedBy = 'admin';
  73. $action->createDate = time();
  74. $action->dueDate = time();
  75. $action->subject = 'Test Subject';
  76. $action->completeDate = time();
  77. $action->complete = 'Yes';
  78. $action->actionDescription = 'Test Body';
  79. // These attributes are context-sensitive and subject to change:
  80. $action->associationId = $contact->id;
  81. $action->associationType = $contact->module;
  82. $action->type = 'email';
  83. $action->visibility = 1;
  84. $action->assignedTo = 'admin';
  85. $action->save();
  86. $track = new TrackEmail();
  87. $track->actionId = $action->id;
  88. $track->uniqueId = md5(uniqid(rand(), true));
  89. $this->assertSaves($track);
  90. $this->assertEquals($contactInitialActionCount + 1,
  91. Yii::app()->db->createCommand()
  92. ->select('COUNT(*)')
  93. ->from('x2_actions')
  94. ->where('associationType = :type AND associationId = :id',
  95. array(
  96. ':type' => $contact->module,
  97. ':id' => $contact->id,
  98. ))
  99. ->queryScalar());
  100. $track->recordEmailOpen();
  101. $this->assertEquals($contactInitialActionCount + 2,
  102. Yii::app()->db->createCommand()
  103. ->select('COUNT(*)')
  104. ->from('x2_actions')
  105. ->where('associationType = :type AND associationId = :id',
  106. array(
  107. ':type' => $contact->module,
  108. ':id' => $contact->id,
  109. ))
  110. ->queryScalar());
  111. $this->assertEmailOpenAction($contact);
  112. }
  113. public function testAccountEmailOpen(){
  114. $account = $this->accounts('testQuote');
  115. $this->assertNullEmailOpenAction($account);
  116. $accountInitialActionCount = Yii::app()->db->createCommand()
  117. ->select('COUNT(*)')
  118. ->from('x2_actions')
  119. ->where('associationType = :type AND associationId = :id',
  120. array(
  121. ':type' => $account->module,
  122. ':id' => $account->id,
  123. ))
  124. ->queryScalar();
  125. $action = new Actions();
  126. $action->completedBy = 'admin';
  127. $action->createDate = time();
  128. $action->dueDate = time();
  129. $action->subject = 'Test Subject';
  130. $action->completeDate = time();
  131. $action->complete = 'Yes';
  132. $action->actionDescription = 'Test Body';
  133. // These attributes are context-sensitive and subject to change:
  134. $action->associationId = $account->id;
  135. $action->associationType = $account->module;
  136. $action->type = 'email';
  137. $action->visibility = 1;
  138. $action->assignedTo = 'admin';
  139. $action->save();
  140. $track = new TrackEmail();
  141. $track->actionId = $action->id;
  142. $track->uniqueId = md5(uniqid(rand(), true));
  143. $this->assertSaves($track);
  144. $this->assertEquals($accountInitialActionCount + 1,
  145. Yii::app()->db->createCommand()
  146. ->select('COUNT(*)')
  147. ->from('x2_actions')
  148. ->where('associationType = :type AND associationId = :id',
  149. array(
  150. ':type' => $account->module,
  151. ':id' => $account->id,
  152. ))
  153. ->queryScalar());
  154. $track->recordEmailOpen();
  155. $this->assertEquals($accountInitialActionCount + 2,
  156. Yii::app()->db->createCommand()
  157. ->select('COUNT(*)')
  158. ->from('x2_actions')
  159. ->where('associationType = :type AND associationId = :id',
  160. array(
  161. ':type' => $account->module,
  162. ':id' => $account->id,
  163. ))
  164. ->queryScalar());
  165. $this->assertEmailOpenAction($account);
  166. }
  167. public function testQuoteEmailOpen(){
  168. $quote = $this->quote('docsTest');
  169. $this->assertNullEmailOpenAction($quote);
  170. $quoteInitialActionCount = Yii::app()->db->createCommand()
  171. ->select('COUNT(*)')
  172. ->from('x2_actions')
  173. ->where('associationType = :type AND associationId = :id',
  174. array(
  175. ':type' => $quote->module,
  176. ':id' => $quote->id,
  177. ))
  178. ->queryScalar();
  179. $action = new Actions();
  180. $action->completedBy = 'admin';
  181. $action->createDate = time();
  182. $action->dueDate = time();
  183. $action->subject = 'Test Subject';
  184. $action->completeDate = time();
  185. $action->complete = 'Yes';
  186. $action->actionDescription = 'Test Body';
  187. // These attributes are context-sensitive and subject to change:
  188. $action->associationId = $quote->id;
  189. $action->associationType = $quote->module;
  190. $action->type = 'email';
  191. $action->visibility = 1;
  192. $action->assignedTo = 'admin';
  193. $action->save();
  194. $track = new TrackEmail();
  195. $track->actionId = $action->id;
  196. $track->uniqueId = md5(uniqid(rand(), true));
  197. $this->assertSaves($track);
  198. $this->assertEquals($quoteInitialActionCount + 1,
  199. Yii::app()->db->createCommand()
  200. ->select('COUNT(*)')
  201. ->from('x2_actions')
  202. ->where('associationType = :type AND associationId = :id',
  203. array(
  204. ':type' => $quote->module,
  205. ':id' => $quote->id,
  206. ))
  207. ->queryScalar());
  208. $track->recordEmailOpen();
  209. $this->assertEquals($quoteInitialActionCount + 2,
  210. Yii::app()->db->createCommand()
  211. ->select('COUNT(*)')
  212. ->from('x2_actions')
  213. ->where('associationType = :type AND associationId = :id',
  214. array(
  215. ':type' => $quote->module,
  216. ':id' => $quote->id,
  217. ))
  218. ->queryScalar());
  219. $this->assertEmailOpenAction($quote);
  220. }
  221. private function assertNullEmailOpenAction($model) {
  222. $this->assertNull(Actions::model()->findByAttributes(array(
  223. 'associationType' => $model->module,
  224. 'associationId' => $model->id,
  225. 'type' => 'emailOpened',
  226. )));
  227. }
  228. private function assertEmailOpenAction($model) {
  229. $action = Actions::model()->findByAttributes(array(
  230. 'associationType' => $model->module,
  231. 'associationId' => $model->id,
  232. 'type' => 'emailOpened',
  233. ));
  234. $this->assertNotNull($action);
  235. //Make sure the module text is correct in the open text
  236. $openText = Modules::displayName(false, $model->module).' has opened the email sent on';
  237. $this->assertNotFalse(strpos($action->actionDescription, $openText));
  238. }
  239. }