PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/x2engine/protected/tests/unit/modules/actions/models/ActionsTest.php

https://gitlab.com/e0/X2CRM
PHP | 284 lines | 167 code | 43 blank | 74 comment | 18 complexity | ea111b667cbad7164905acf9db251918 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.models.*');
  38. Yii::import('application.modules.actions.models.*');
  39. Yii::import('application.modules.groups.models.*');
  40. Yii::import('application.modules.users.models.*');
  41. Yii::import('application.components.*');
  42. Yii::import('application.components.permissions.*');
  43. Yii::import('application.components.X2Settings.*');
  44. Yii::import('application.components.sortableWidget.profileWidgets.*');
  45. Yii::import('application.components.sortableWidget.recordViewWidgets.*');
  46. /**
  47. * Test for the Actions class
  48. * @package application.tests.unit.modules.actions.models
  49. * @author Demitri Morgan <demitri@x2engine.com>
  50. */
  51. class ActionsTest extends X2DbTestCase {
  52. public $fixtures = array(
  53. 'actions'=>array ('Actions', '.ActionsTest'),
  54. 'users'=> 'User',
  55. 'profiles'=> 'Profile',
  56. 'groupToUser'=>array ('GroupToUser', '.ActionsTest'),
  57. 'groups'=>array ('Groups', '.ActionsTest'),
  58. );
  59. /**
  60. * Test special validation that avoids empty association when the type is
  61. * something meant to be associated, i.e. a logged call, note, etc.
  62. */
  63. public function testValidate() {
  64. $action = new Actions();
  65. $action->type = 'call';
  66. $action->actionDescription = 'Contacted. Will call back later';
  67. $this->assertFalse($action->validate());
  68. $this->assertTrue($action->hasErrors('associationId'));
  69. $this->assertTrue($action->hasErrors('associationType'));
  70. // Do the same thing but with "None" association type. Validation should fail.
  71. $action = new Actions();
  72. $action->type = 'call';
  73. $action->associationType = 'None';
  74. $this->assertFalse($action->validate());
  75. $this->assertTrue($action->hasErrors('associationId'));
  76. $this->assertTrue($action->hasErrors('associationType'));
  77. }
  78. public function testIsAssignedTo () {
  79. $action = $this->actions('action1');
  80. // test assignedTo field consisting of single username
  81. $this->assertTrue ($action->isAssignedTo ('testuser'));
  82. $this->assertFalse ($action->isAssignedTo ('testuser2'));
  83. $action = $this->actions('action2');
  84. // test assignedTo field consisting of a group id
  85. $this->assertTrue ($action->isAssignedTo ('testuser'));
  86. $this->assertFalse ($action->isAssignedTo ('testuser2'));
  87. $action = $this->actions('action3');
  88. // test assignedTo field consisting of username and group id
  89. $this->assertTrue ($action->isAssignedTo ('testuser'));
  90. $this->assertTrue ($action->isAssignedTo ('testuser2'));
  91. $this->assertFalse ($action->isAssignedTo ('testuser3'));
  92. $action = $this->actions('action4');
  93. // test assignedTo field consisting of 'Anyone'
  94. $this->assertTrue ($action->isAssignedTo ('testuser4'));
  95. $this->assertFalse ($action->isAssignedTo ('testuser4', true));
  96. // test assignedTo field consisting of '' (i.e. no one)
  97. $this->assertTrue ($action->isAssignedTo ('testuser4'));
  98. $this->assertFalse ($action->isAssignedTo ('testuser4', true));
  99. }
  100. public function testGetProfilesOfAssignees () {
  101. // action assignedTo field consists of username and group id
  102. $action = $this->actions('action3');
  103. $profiles = $action->getProfilesOfAssignees ();
  104. // this should return profile for username and all profiles in group, without duplicates
  105. $profileUsernames = array_map (function ($a) { return $a->username; }, $profiles);
  106. X2_TEST_DEBUG_LEVEL > 1 && print ('count ($profiles) = ');
  107. X2_TEST_DEBUG_LEVEL > 1 && print (count ($profiles)."\n");
  108. X2_TEST_DEBUG_LEVEL > 1 && print ('$profileUsernames = ');
  109. X2_TEST_DEBUG_LEVEL > 1 && print_r ($profileUsernames);
  110. $this->assertTrue (count ($profiles) === 2);
  111. $this->assertTrue (in_array ('testuser', $profileUsernames));
  112. $this->assertTrue (in_array ('testuser2', $profileUsernames));
  113. /*
  114. action assignedTo field consists of username and group id. Here the username is included
  115. twice: once explicitly in the assignedTo field and a second time, implicitly, by its
  116. membership to the group.
  117. */
  118. $action = $this->actions('action6');
  119. $profiles = $action->getProfilesOfAssignees ();
  120. // this should return profile for username and all profiles in group, without duplicates
  121. $profileUsernames = array_map (function ($a) { return $a->username; }, $profiles);
  122. X2_TEST_DEBUG_LEVEL > 1 && print ('count ($profiles) = ');
  123. X2_TEST_DEBUG_LEVEL > 1 && print (count ($profiles)."\n");
  124. X2_TEST_DEBUG_LEVEL > 1 && print ('$profileUsernames = ');
  125. X2_TEST_DEBUG_LEVEL > 1 && print_r ($profileUsernames);
  126. $this->assertTrue (count ($profiles) === 2);
  127. $this->assertTrue (in_array ('testuser', $profileUsernames));
  128. $this->assertTrue (in_array ('admin', $profileUsernames));
  129. }
  130. public function testGetAssignees () {
  131. // action assignedTo field consists of username and group id
  132. $action = $this->actions('action3');
  133. $assignees = $action->getAssignees (true);
  134. X2_TEST_DEBUG_LEVEL > 1 && print ('count ($assignees) = ');
  135. X2_TEST_DEBUG_LEVEL > 1 && print (count ($assignees)."\n");
  136. $this->assertTrue (count ($assignees) === 2);
  137. $this->assertTrue (in_array ('testuser', $assignees));
  138. $this->assertTrue (in_array ('testuser2', $assignees));
  139. /*
  140. action assignedTo field consists of username and group id. Here the username is included
  141. twice: once explicitly in the assignedTo field and a second time, implicitly, by its
  142. membership to the group.
  143. */
  144. $action = $this->actions('action6');
  145. /*
  146. here assignees usernames are retrieved, if a group id is in the assignedTo string,
  147. usernames of all users in that group are also retrieved. duplicate usernames should
  148. get removed.
  149. */
  150. $assignees = $action->getAssignees (true);
  151. X2_TEST_DEBUG_LEVEL > 1 && print ('count ($assignees) = ');
  152. X2_TEST_DEBUG_LEVEL > 1 && print (count ($assignees)."\n");
  153. $this->assertTrue (count ($assignees) === 2);
  154. $this->assertTrue (in_array ('testuser', $assignees));
  155. $this->assertTrue (in_array ('admin', $assignees));
  156. }
  157. public function testCreateNotification () {
  158. // assigned to testuser and group 1
  159. $action = $this->actions('action6');
  160. $notifs = $action->createNotifications ('assigned');
  161. X2_TEST_DEBUG_LEVEL > 1 && print (count ($notifs));
  162. $this->assertTrue (count ($notifs) === 2);
  163. $notifAssignees = array_map (function ($a) { return $a->user; }, $notifs);
  164. $this->assertTrue (in_array ('admin', $notifAssignees));
  165. $this->assertTrue (in_array ('testuser', $notifAssignees));
  166. $notifs = $action->createNotifications ('me');
  167. $this->assertTrue (count ($notifs) === 1);
  168. $notifAssignees = array_map (function ($a) { return $a->user; }, $notifs);
  169. X2_TEST_DEBUG_LEVEL > 1 && print_r ($notifAssignees);
  170. $this->assertTrue (in_array ('Guest', $notifAssignees));
  171. $notifs = $action->createNotifications ('both');
  172. $this->assertTrue (count ($notifs) === 3);
  173. $notifAssignees = array_map (function ($a) { return $a->user; }, $notifs);
  174. $this->assertTrue (in_array ('admin', $notifAssignees));
  175. $this->assertTrue (in_array ('testuser', $notifAssignees));
  176. $this->assertTrue (in_array ('Guest', $notifAssignees));
  177. }
  178. public function testChangeCompleteState () {
  179. TestingAuxLib::suLogin ('admin');
  180. X2_TEST_DEBUG_LEVEL > 1 && print (Yii::app()->user->name ."\n");
  181. X2_TEST_DEBUG_LEVEL > 1 && print ((int) Yii::app()->params->isAdmin);
  182. X2_TEST_DEBUG_LEVEL > 1 && print ("\n");
  183. $action = $this->actions('action6');
  184. $completedNum = Actions::changeCompleteState ('complete', array ($action->id));
  185. $this->assertEquals (1, $completedNum);
  186. $action = Actions::model()->findByPk ($action->id);
  187. X2_TEST_DEBUG_LEVEL > 1 && print ($action->complete."\n");
  188. $this->assertTrue ($action->complete === 'Yes');
  189. Actions::changeCompleteState ('uncomplete', array ($action->id));
  190. $action = Actions::model()->findByPk ($action->id);
  191. $this->assertTrue ($action->complete === 'No');
  192. }
  193. public function testDeleteOldNotifications () {
  194. TestingAuxLib::suLogin ('admin');
  195. // assigned to testuser
  196. $action = $this->actions('action1');
  197. $reminders = $action->getReminders (true);
  198. foreach ($reminders as $reminder) $this->assertTrue ($reminder->delete ());
  199. $deleteOldNotifications = TestingAuxLib::setPublic ($action, 'deleteOldNotifications');
  200. $this->assertEquals (0, count ($action->getReminders (true)));
  201. $action->createNotifications ('assigned', 1234, 'action_reminder');
  202. $this->assertGreaterThan (0, count ($action->getReminders (true)));
  203. $deleteOldNotifications ('me');
  204. $this->assertGreaterThan (0, count ($action->getReminders (true)));
  205. $deleteOldNotifications ('assigned');
  206. $this->assertEquals (0, count ($action->getReminders (true)));
  207. }
  208. public function testUpdateWithNotifications () {
  209. TestingAuxLib::loadX2NonWebUser ();
  210. TestingAuxLib::suLogin ('admin');
  211. // assigned to testuser
  212. $action = $this->actions('action1');
  213. $reminders = $action->getReminders (true);
  214. foreach ($reminders as $reminder) $this->assertTrue ($reminder->delete ());
  215. $this->assertEquals (0, count ($action->getReminders (true)));
  216. // ensure that we can create a reminder
  217. $action->reminder = true;
  218. $action->notificationUsers = 'assigned';
  219. $action->notificationTime = 1234;
  220. $this->assertSaves ($action);
  221. $this->assertEquals (1, count ($action->getReminders (true)));
  222. $reminders = $action->getReminders (true);
  223. $assignees = array_map (function ($reminder) {
  224. return $reminder->user;
  225. }, $reminders);
  226. $this->assertEquals (array ('testuser'), $assignees);
  227. // now ensure that we can create another reminder and that the old reminder was deleted
  228. TestingAuxLib::suLogin ('testuser');
  229. $action->reminder = true;
  230. $action->notificationUsers = 'assigned';
  231. $action->notificationTime = 1234;
  232. $this->assertSaves ($action);
  233. $this->assertEquals (1, count ($action->getReminders (true)));
  234. $reminders = $action->getReminders (true);
  235. $assignees = array_map (function ($reminder) {
  236. return $reminder->user;
  237. }, $reminders);
  238. $this->assertEquals (array ('testuser'), $assignees);
  239. }
  240. }
  241. ?>