PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/e0/X2CRM
PHP | 309 lines | 217 code | 26 blank | 66 comment | 8 complexity | bf3ee0e6d42fd97fc3a0254a6b75b9f0 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.contacts.models.*');
  38. Yii::import ('application.modules.accounts.models.*');
  39. /**
  40. * @package application.tests.unit.models
  41. * @author Demitri Morgan <demitri@x2engine.com>
  42. */
  43. class AdminTest extends X2DbTestCase {
  44. private static $_appFileUtilState = array ();
  45. private static $_adminState = array ();
  46. public static function referenceFixtures () {
  47. return array (
  48. 'contacts' => 'Contacts',
  49. 'tags' => 'Tags',
  50. );
  51. }
  52. public static function setUpBeforeClass () {
  53. self::$_appFileUtilState['alwaysCurl'] = AppFileUtil::$alwaysCurl;
  54. self::$_appFileUtilState['neverCurl'] = AppFileUtil::$neverCurl;
  55. self::$_adminState['unique_id'] = Yii::app()->settings->unique_id;
  56. return parent::setUpBeforeClass ();
  57. }
  58. public static function tearDownAfterClass () {
  59. AppFileUtil::$alwaysCurl = self::$_appFileUtilState['alwaysCurl'];
  60. AppFileUtil::$neverCurl = self::$_appFileUtilState['neverCurl'];
  61. Yii::app()->settings->unique_id = self::$_adminState['unique_id'];
  62. Yii::app()->settings->save ();
  63. return parent::tearDownAfterClass ();
  64. }
  65. public function testCountEmail() {
  66. $admin = Yii::app()->settings;
  67. $admin->emailCount = 0;
  68. $admin->emailInterval = 2;
  69. $admin->update(array('emailCount','emailInterval'));
  70. $now = time();
  71. // This should register five emails as having been sent:
  72. $admin->countEmail(5);
  73. $this->assertEquals($now,$admin->emailStartTime);
  74. $this->assertEquals(5,$admin->emailCount);
  75. // One more:
  76. $admin->countEmail();
  77. $this->assertEquals(6,$admin->emailCount);
  78. sleep(3);
  79. // After the interval ends, the count should be reset
  80. $now = time();
  81. $admin->countEmail(3);
  82. $this->assertEquals($now,$admin->emailStartTime);
  83. $this->assertEquals(3,$admin->emailCount);
  84. }
  85. public function testEmailCountWillExceedLimit() {
  86. $admin = Yii::app()->settings;
  87. $admin->emailCount = 0;
  88. $admin->emailInterval = 5;
  89. $admin->emailBatchSize = 5;
  90. $admin->update(array('emailCount','emailInterval','emailBatchSize'));
  91. $admin->countEmail(4);
  92. // One more won't kill us
  93. $this->assertFalse($admin->emailCountWillExceedLimit(1));
  94. // Two more will exceed the limit
  95. $this->assertTrue($admin->emailCountWillExceedLimit(2));
  96. }
  97. /**
  98. * Ensure that invalid license key causes exception to be thrown
  99. */
  100. public function testGetLicenseKeyInfoCurlInvalid () {
  101. Yii::app()->settings->unique_id = 'invalid';
  102. AppFileUtil::$neverCurl = false;
  103. AppFileUtil::$alwaysCurl = true;
  104. $this->assertTrue (Yii::app()->cache2->flush ());
  105. $this->assertEquals (array (
  106. 'errors' => 'invalid'
  107. ), Yii::app()->settings->getLicenseKeyInfo (true)); // x2planet request
  108. Yii::app()->settings->unique_id = VALID_LICENSE_KEY_PRO;
  109. $this->assertEquals (array (
  110. 'errors' => 'invalid'
  111. ), Yii::app()->settings->getLicenseKeyInfo (true)); // file cache check
  112. $this->assertEquals (array (
  113. 'errors' => 'invalid'
  114. ), Yii::app()->settings->getLicenseKeyInfo (false)); // property cache check
  115. }
  116. /**
  117. * Ensure that invalid license key causes exception to be thrown
  118. */
  119. public function testGetLicenseKeyInfoFileGetContentsInvalid () {
  120. Yii::app()->settings->unique_id = 'invalid';
  121. AppFileUtil::$neverCurl = true;
  122. AppFileUtil::$alwaysCurl = false;
  123. $this->assertTrue (Yii::app()->cache2->flush ());
  124. $this->assertEquals (array (
  125. 'errors' => 'invalid'
  126. ), Yii::app()->settings->getLicenseKeyInfo (true));
  127. Yii::app()->settings->unique_id = VALID_LICENSE_KEY_PRO;
  128. $this->assertEquals (array (
  129. 'errors' => 'invalid'
  130. ), Yii::app()->settings->getLicenseKeyInfo (true));
  131. $this->assertEquals (array (
  132. 'errors' => 'invalid'
  133. ), Yii::app()->settings->getLicenseKeyInfo (false));
  134. }
  135. public function testGetLicenseKeyInfoCurlMissing () {
  136. Yii::app()->settings->unique_id = null;
  137. AppFileUtil::$neverCurl = false;
  138. AppFileUtil::$alwaysCurl = true;
  139. $this->assertTrue (Yii::app()->cache2->flush ());
  140. $this->assertEquals (array (), Yii::app()->settings->getLicenseKeyInfo (true));
  141. Yii::app()->settings->unique_id = 'invalid';
  142. $this->assertEquals (array (), Yii::app()->settings->getLicenseKeyInfo (true));
  143. $this->assertEquals (array (), Yii::app()->settings->getLicenseKeyInfo (false));
  144. }
  145. /**
  146. * Ensure that invalid license key causes exception to be thrown
  147. */
  148. public function testGetLicenseKeyInfoFileGetContentsMissing () {
  149. Yii::app()->settings->unique_id = null;
  150. AppFileUtil::$neverCurl = true;
  151. AppFileUtil::$alwaysCurl = false;
  152. $this->assertTrue (Yii::app()->cache2->flush ());
  153. $this->assertEquals (array (), Yii::app()->settings->getLicenseKeyInfo (true));
  154. Yii::app()->settings->unique_id = 'invalid';
  155. $this->assertEquals (array (), Yii::app()->settings->getLicenseKeyInfo (true));
  156. $this->assertEquals (array (), Yii::app()->settings->getLicenseKeyInfo (false));
  157. }
  158. /**
  159. * Ensure that we can retrieve license key info with valid license keys
  160. */
  161. public function testGetLicenseKeyInfoValid () {
  162. $url = X2_TESTING_UPDATE_SERVER . '/installs/registry/getLicenseKeyInfo';
  163. foreach (array (false, true) as $useCurl) {
  164. foreach (array (VALID_LICENSE_KEY_PRO, VALID_LICENSE_KEY_PLA) as $key) {
  165. X2_TEST_DEBUG_LEVEL > 1 &&
  166. /**/println (
  167. 'getting info for '.$key.' '.($useCurl ? '' : 'not ').'using curl');
  168. AppFileUtil::$neverCurl = !$useCurl;
  169. AppFileUtil::$alwaysCurl = $useCurl;
  170. $this->assertEquals ($useCurl, AppFileUtil::tryCurl ($url));
  171. Yii::app()->settings->unique_id = $key;
  172. $this->assertTrue (Yii::app()->cache2->flush ());
  173. $licenseKey = Yii::app()->settings->getLicenseKeyInfo (true);
  174. X2_TEST_DEBUG_LEVEL > 1 && print_r ($licenseKey);
  175. $this->assertTrue (isset ($licenseKey['dateExpires']));
  176. $this->assertTrue (isset ($licenseKey['maxUsers']));
  177. $this->assertTrue (is_numeric ($licenseKey['dateExpires']));
  178. $this->assertTrue (is_numeric ($licenseKey['maxUsers']));
  179. // now with caching
  180. Yii::app()->settings->unique_id = null;
  181. $licenseKey = Yii::app()->settings->getLicenseKeyInfo (true);
  182. X2_TEST_DEBUG_LEVEL > 1 && print_r ($licenseKey);
  183. $this->assertTrue (isset ($licenseKey['dateExpires']));
  184. $this->assertTrue (isset ($licenseKey['maxUsers']));
  185. $this->assertTrue (is_numeric ($licenseKey['dateExpires']));
  186. $this->assertTrue (is_numeric ($licenseKey['maxUsers']));
  187. }
  188. }
  189. }
  190. /**
  191. * Ensure that both valid and invalid license keys renders without error
  192. */
  193. public function testLicenseKeyInfoRendering () {
  194. $url = X2_TESTING_UPDATE_SERVER . '/installs/registry/getLicenseKeyInfo';
  195. foreach (array (false, true) as $useCurl) {
  196. foreach (array ('invalid', VALID_LICENSE_KEY_PRO, VALID_LICENSE_KEY_PLA) as $key) {
  197. X2_TEST_DEBUG_LEVEL > 1 &&
  198. /**/println (
  199. 'rendering info for '.$key.' '.($useCurl ? '' : 'not ').'using curl');
  200. AppFileUtil::$neverCurl = !$useCurl;
  201. AppFileUtil::$alwaysCurl = $useCurl;
  202. $this->assertEquals ($useCurl, AppFileUtil::tryCurl ($url));
  203. Yii::app()->settings->unique_id = $key;
  204. $this->assertTrue (Yii::app()->cache2->flush ());
  205. $this->assertTrue (
  206. is_string (
  207. $oldDate = Yii::app()->settings->renderProductKeyExpirationDate (true)));
  208. $this->assertTrue (
  209. is_string ($oldMaxUsers = Yii::app()->settings->renderMaxUsers (true)));
  210. if ($key !== 'invalid') {
  211. $this->assertEquals (1, preg_match ('/strong/', $oldMaxUsers));
  212. $this->assertEquals (1, preg_match ('/strong/', $oldDate));
  213. $this->assertEquals (0, preg_match ('/error-text/', $oldDate));
  214. Yii::app()->settings->unique_id = 'invalid';
  215. } else {
  216. $this->assertEquals ('', $oldMaxUsers);
  217. $this->assertEquals (1, preg_match ('/error-text/', $oldDate));
  218. Yii::app()->settings->unique_id = VALID_LICENSE_KEY_PRO;
  219. }
  220. // now with caching
  221. $this->assertEquals (
  222. $oldDate, Yii::app()->settings->renderProductKeyExpirationDate (true));
  223. $this->assertEquals (
  224. $oldMaxUsers,Yii::app()->settings->renderMaxUsers (true));
  225. }
  226. }
  227. }
  228. /**
  229. * Ensure that license key info cache behaves as expected
  230. */
  231. public function testLicenseKeyInfoCaching () {
  232. $admin = Admin::model ()->findByPk (1);
  233. $this->assertTrue (Yii::app()->cache2->flush ());
  234. $admin->unique_id = VALID_LICENSE_KEY_PRO;
  235. $licenseKey = $admin->getLicenseKeyInfo (true);
  236. X2_TEST_DEBUG_LEVEL > 1 && print_r ($licenseKey);
  237. $this->assertTrue (isset ($licenseKey['dateExpires']));
  238. $this->assertTrue (isset ($licenseKey['maxUsers']));
  239. $this->assertTrue (is_numeric ($licenseKey['dateExpires']));
  240. $this->assertTrue (is_numeric ($licenseKey['maxUsers']));
  241. // cache is still valid since we haven't saved yet
  242. $admin->unique_id = 'invalid';
  243. $licenseKey = $admin->getLicenseKeyInfo (true);
  244. X2_TEST_DEBUG_LEVEL > 1 && print_r ($licenseKey);
  245. $this->assertTrue (isset ($licenseKey['dateExpires']));
  246. $this->assertTrue (isset ($licenseKey['maxUsers']));
  247. $this->assertTrue (is_numeric ($licenseKey['dateExpires']));
  248. $this->assertTrue (is_numeric ($licenseKey['maxUsers']));
  249. // saving a different license key should invalidate the cache
  250. $this->assertSaves ($admin);
  251. $licenseKey = $admin->getLicenseKeyInfo (true);
  252. $this->assertTrue (!isset ($licenseKey['dateExpires']));
  253. $this->assertTrue (!isset ($licenseKey['maxUsers']));
  254. $this->assertTrue (isset ($licenseKey['errors']));
  255. $this->assertEquals ('invalid', $licenseKey['errors']);
  256. }
  257. public function testDisableAutomaticRecordTagging () {
  258. Yii::app()->db->createCommand ("delete from x2_tags where 1");
  259. $admin = Yii::app()->settings;
  260. $admin->disableAutomaticRecordTagging = true;
  261. $this->assertUpdates ($admin, array ('disableAutomaticRecordTagging'));
  262. $contact = new Contacts;
  263. $contact->setAttributes (array (
  264. 'firstName' => 'test',
  265. 'lastName' => 'test',
  266. 'visibility' => 1,
  267. 'backgroundInfo' => '#tag0 #tag1 #tag2',
  268. ));
  269. $this->assertSaves ($contact);
  270. $this->assertEquals (0, (int) Yii::app()->db->createCommand ("
  271. select count(*) from x2_tags
  272. ")->queryScalar ());
  273. $admin->disableAutomaticRecordTagging = false;
  274. $this->assertUpdates ($admin, array ('disableAutomaticRecordTagging'));
  275. $this->assertSaves ($contact);
  276. $this->assertEquals (array ('#tag0', '#tag1', '#tag2'), Yii::app()->db->createCommand ()
  277. ->select ('tag')
  278. ->from ('x2_tags')
  279. ->order ('tag asc')
  280. ->queryColumn ());
  281. }
  282. }
  283. ?>