PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/modules/zurmo/tests/unit/ItemTest.php

https://bitbucket.org/zurmo/zurmo/
PHP | 274 lines | 187 code | 16 blank | 71 comment | 0 complexity | 3f6554bd679fcc079b1c01483828ccde MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. class ItemTest extends ZurmoBaseTest
  37. {
  38. public static function setUpBeforeClass()
  39. {
  40. parent::setUpBeforeClass();
  41. SecurityTestHelper::createSuperAdmin();
  42. }
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. Yii::app()->user->userModel = User::getByUsername('super');
  47. }
  48. /**
  49. * This test serves to confirm createdBy/ModifiedBy user is setting/getting properly. This test was added
  50. * after the import work as part of a refactor to allow for readOnly attributes to be set externally.
  51. */
  52. public function testNewModelCreatedAndModifiedByUserIsNull()
  53. {
  54. $account = new Account();
  55. $this->assertTrue($account->createdByUser->id < 0);
  56. $this->assertTrue($account->modifiedByUser->id < 0);
  57. $this->assertFalse(array_key_exists('createdByUser', $account->originalAttributeValues));
  58. $this->assertFalse(array_key_exists('modifiedByUser', $account->originalAttributeValues));
  59. //Add values and save. This account is being created.
  60. $account->name = 'aTest';
  61. $account->owner = User::getByUsername('super');
  62. $this->assertTrue($account->save());
  63. $this->assertFalse(array_key_exists('createdByUser', $account->originalAttributeValues));
  64. $this->assertFalse(array_key_exists('modifiedByUser', $account->originalAttributeValues));
  65. $this->assertTrue($account->createdByUser->id > 0);
  66. $this->assertTrue($account->modifiedByUser->id > 0);
  67. //Change the value and save. This time the account is being modified.
  68. $account->name = 'aTestb';
  69. $this->assertTrue($account->save());
  70. $this->assertFalse(array_key_exists('modifiedByUser', $account->originalAttributeValues));
  71. }
  72. public function testCreationAndModificationTimes()
  73. {
  74. $user = UserTestHelper::createBasicUser('Billy');
  75. $account = new Account();
  76. $createdTime = time();
  77. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  78. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime), 2);
  79. sleep(3); // Sleeps are bad in tests, but I need some time to pass
  80. // to test these time stamps. The 3's here just need to be more
  81. // than the 2's in the asserts. Using 2 and 1 meant it failed
  82. // occasionally because the second would tick over just at the
  83. // wrong time.
  84. $account->owner = $user;
  85. $account->name = 'Test Account';
  86. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  87. $this->assertWithinTolerance(time(), DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime), 2);
  88. sleep(3);
  89. $account->officePhone = '1234567890';
  90. $lastModifiedTime = time();
  91. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  92. $this->assertWithinTolerance($lastModifiedTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime), 2);
  93. $this->assertTrue($account->save());
  94. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  95. $this->assertWithinTolerance($lastModifiedTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime), 2);
  96. $id = $account->id;
  97. unset($account);
  98. sleep(2);
  99. $account = Account::getById($id);
  100. $this->assertEquals('Test Account', $account->name);
  101. $this->assertEquals('1234567890', $account->officePhone);
  102. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  103. $this->assertWithinTolerance($lastModifiedTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime), 2);
  104. unset($account);
  105. sleep(3);
  106. $account = Account::getById($id);
  107. $this->assertEquals('Test Account', $account->name);
  108. $this->assertEquals('1234567890', $account->officePhone);
  109. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  110. $this->assertWithinTolerance($lastModifiedTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime), 2);
  111. unset($account);
  112. sleep(3);
  113. $account = Account::getById($id);
  114. $contact = ContactTestHelper::createContactByNameForOwner('contactTest', $user);
  115. $account->contacts->add($contact);
  116. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  117. $this->assertTrue($account->save());
  118. $this->assertWithinTolerance($createdTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->createdDateTime), 2);
  119. $this->assertNotEquals ($lastModifiedTime, DateTimeUtil::convertDbFormatDateTimeToTimestamp($account->modifiedDateTime));
  120. }
  121. /**
  122. * @depends testCreationAndModificationTimes
  123. */
  124. public function testExtraItemsCreatedOnModelInstantiation()
  125. {
  126. $countBefore = intval(ZurmoRedBean::getCell("select count(*) from item;"));
  127. $account = new Account();
  128. $countAfter = intval(ZurmoRedBean::getCell("select count(*) from item;"));
  129. $this->assertEquals($countBefore, $countAfter);
  130. }
  131. /**
  132. * @depends testCreationAndModificationTimes
  133. * @expectedException NotSupportedException
  134. */
  135. public function testItemReadOnlyFieldsCreatedTime()
  136. {
  137. $account = new Account();
  138. $createdTime = time();
  139. $this->assertTrue($account->isAttributeReadOnly('createdDateTime'));
  140. $account->createdDateTime = time() + 123123;
  141. }
  142. /**
  143. * @depends testCreationAndModificationTimes
  144. * @expectedException NotSupportedException
  145. */
  146. public function testItemReadOnlyFieldsCreatedUser()
  147. {
  148. $user = User::getByUsername('billy');
  149. $account = new Account();
  150. $this->assertTrue($account->isAttributeReadOnly('createdByUser'));
  151. $account->createdByUser = $user;
  152. }
  153. /**
  154. * @depends testCreationAndModificationTimes
  155. * @expectedException NotSupportedException
  156. */
  157. public function testItemReadOnlyFieldsModifiedTime()
  158. {
  159. $account = new Account();
  160. $createdTime = time();
  161. $this->assertTrue($account->isAttributeReadOnly('modifiedDateTime'));
  162. $account->modifiedDateTime = time() + 123123;
  163. }
  164. /**
  165. * @depends testCreationAndModificationTimes
  166. * @expectedException NotSupportedException
  167. */
  168. public function testItemReadOnlyFieldsModifiedUser()
  169. {
  170. $user = User::getByUsername('billy');
  171. $account = new Account();
  172. $account->modifiedByUser = $user;
  173. }
  174. /**
  175. * @depends testItemReadOnlyFieldsModifiedUser
  176. */
  177. public function testItemReadOnlyChangeScenarioSoCanPopulate()
  178. {
  179. Yii::app()->user->userModel = User::getByUsername('super');
  180. $dbDateTime1 = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 200);
  181. $dbDateTime2 = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 300);
  182. $dbDateTime3 = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 400);
  183. $jimmy = UserTestHelper::createBasicUser('Jimmy');
  184. $user = User::getByUsername('billy');
  185. $account = new Account();
  186. $account->setScenario('importModel');
  187. $account->createdByUser = $user;
  188. $account->modifiedByUser = $user;
  189. $account->createdDateTime = $dbDateTime1;
  190. $account->modifiedDateTime = $dbDateTime2;
  191. $account->owner = Yii::app()->user->userModel;
  192. $account->name = 'someName';
  193. $this->assertTrue($account->save());
  194. $accountId = $account->id;
  195. $account->forget();
  196. $account = Account::getById($accountId);
  197. $this->assertEquals($user, $account->createdByUser);
  198. $this->assertEquals($user, $account->modifiedByUser);
  199. $this->assertEquals($dbDateTime1, $account->createdDateTime);
  200. $this->assertEquals($dbDateTime2, $account->modifiedDateTime);
  201. $account->name = 'aNewName';
  202. $this->assertTrue($account->save());
  203. $account->forget();
  204. //Now test that the attempt to change createdByUser and modifiedUser on an existing model will not work.
  205. //even when there are read only override permissions set.
  206. $account = Account::getById($accountId);
  207. $this->assertEquals($user, $account->createdByUser);
  208. $this->assertEquals(Yii::app()->user->userModel, $account->modifiedByUser);
  209. $this->assertNotEquals($dbDateTime2, $account->modifiedDateTime);
  210. $this->assertNotEquals($dbDateTime3, $account->modifiedDateTime);
  211. }
  212. /**
  213. * @depends testItemReadOnlyChangeScenarioSoCanPopulate
  214. */
  215. public function testCreatedByAndModifiedByUsersPopulateCorrectly()
  216. {
  217. $super = User::getByUsername('super');
  218. Yii::app()->user->userModel = $super;
  219. $user = User::getByUsername('billy');
  220. $account = new Account();
  221. $account->name = 'aNewDawn Inc';
  222. $account->owner = $user;
  223. $this->assertTrue($account->save());
  224. $account = Account::getById($account->id);
  225. $this->assertEquals($super, $account->createdByUser);
  226. $this->assertEquals($super, $account->modifiedByUser);
  227. Yii::app()->user->userModel = $user;
  228. $account->name = 'aSecondDawn Inc.';
  229. $this->assertTrue($account->save());
  230. $account = Account::getById($account->id);
  231. $this->assertEquals($super, $account->createdByUser);
  232. $this->assertEquals($user, $account->modifiedByUser);
  233. }
  234. public function testWorkflowsToProcessAfterSave()
  235. {
  236. $account = new Account();
  237. $workflow = new Workflow();
  238. $workflow->setModuleClassName('something');
  239. $account->addWorkflowToProcessAfterSave($workflow);
  240. $workflows = $account->getWorkflowsToProcessAfterSave();
  241. $this->assertEquals(1, count($workflows));
  242. $this->assertEquals('something', $workflows[0]->getModuleClassName());
  243. }
  244. public function testReadOnlyFieldsOnSearchScenario()
  245. {
  246. $super = User::getByUsername('super');
  247. Yii::app()->user->userModel = $super;
  248. $account = new Account(false);
  249. $account->setScenario('searchModel');
  250. $account->name = 'aTestAccountForTestReadOnlyFieldsOnSearchScenario';
  251. $account->owner = $super;
  252. $account->createdByUser = $super;
  253. $account->modifiedByUser = $super;
  254. $account->validate();
  255. $this->assertFalse($account->hasErrors());
  256. }
  257. }
  258. ?>