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

/.dev/tests/Classes/Model/Repo/Profile.php

https://github.com/istran/core
PHP | 476 lines | 315 code | 49 blank | 112 comment | 5 complexity | 7f7c2aba9dcadd7e73443d9808b11fac MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * XLite\Model\Repo\Profile class tests
  5. *
  6. * @category LiteCommerce
  7. * @package Tests
  8. * @subpackage Classes
  9. * @author Creative Development LLC <info@cdev.ru>
  10. * @copyright Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
  11. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  12. * @version GIT: $Id: 5f9d7fa3bf9f6754343b00cae8a3a038ae3e2417 $
  13. * @link http://www.litecommerce.com/
  14. * @see ____file_see____
  15. * @since 3.0.0
  16. */
  17. class XLite_Tests_Model_Repo_Profile extends XLite_Tests_TestCase
  18. {
  19. protected $testSearchData = array(
  20. // Test dataset #1
  21. 0 => array(
  22. 'cnd' => array(
  23. 'profile_id' => 4,
  24. ),
  25. 'result' => array(
  26. 'ids' => array(4),
  27. ),
  28. ),
  29. // Test dataset #2
  30. 1 => array(
  31. 'cnd' => array(
  32. 'order_id' => 1,
  33. ),
  34. 'result' => array(
  35. 'ids' => array(8),
  36. ),
  37. ),
  38. // Test dataset #3
  39. 2 => array(
  40. 'cnd' => array(
  41. 'referer' => 'google',
  42. ),
  43. 'result' => array(
  44. 'ids' => array(2, 3),
  45. ),
  46. ),
  47. // Test dataset #4
  48. 3 => array(
  49. 'cnd' => array(
  50. 'membership' => 'pending_membership',
  51. ),
  52. 'result' => array(
  53. 'ids' => array(3, 5),
  54. ),
  55. ),
  56. // Test dataset #5
  57. 4 => array(
  58. 'cnd' => array(
  59. 'membership' => 2,
  60. ),
  61. 'result' => array(
  62. 'ids' => array(4, 5),
  63. ),
  64. ),
  65. // Test dataset #6
  66. 5 => array(
  67. 'cnd' => array(
  68. 'membership' => 1,
  69. ),
  70. 'result' => array(
  71. 'isEmpty' => true
  72. ),
  73. ),
  74. // Test dataset #7
  75. 6 => array(
  76. 'cnd' => array(
  77. 'membership' => 2,
  78. 'user_type' => 'A',
  79. ),
  80. 'result' => array(
  81. 'ids' => array(1, 7),
  82. ),
  83. ),
  84. // Test dataset #8
  85. 7 => array(
  86. 'cnd' => array(
  87. 'language' => 'de',
  88. ),
  89. 'result' => array(
  90. 'ids' => array(4),
  91. ),
  92. ),
  93. // Test dataset #9
  94. 8 => array(
  95. 'cnd' => array(
  96. 'pattern' => 'Patrick Smith',
  97. ),
  98. 'result' => array(
  99. 'ids' => array(3, 4),
  100. ),
  101. ),
  102. // Test dataset #10
  103. 9 => array(
  104. 'cnd' => array(
  105. 'pattern' => 'John Smith',
  106. ),
  107. 'result' => array(
  108. 'ids' => array(2),
  109. ),
  110. ),
  111. // Test dataset #11
  112. 10 => array(
  113. 'cnd' => array(
  114. 'pattern' => 'Smith John',
  115. ),
  116. 'result' => array(
  117. 'ids' => array(2, 3),
  118. ),
  119. ),
  120. // Test dataset #12
  121. 11 => array(
  122. 'cnd' => array(
  123. 'phone' => '76543',
  124. ),
  125. 'result' => array(
  126. 'ids' => array(2, 3, 4, 5, 6, 7),
  127. ),
  128. ),
  129. // Test dataset #13
  130. 12 => array(
  131. 'cnd' => array(
  132. 'country' => 'FR',
  133. ),
  134. 'result' => array(
  135. 'ids' => array(4),
  136. ),
  137. ),
  138. // Test dataset #14
  139. 13 => array(
  140. 'cnd' => array(
  141. 'state' => 37,
  142. ),
  143. 'result' => array(
  144. 'ids' => array(5),
  145. ),
  146. ),
  147. // Test dataset #15
  148. 14 => array(
  149. 'cnd' => array(
  150. 'address_pattern' => 'Paris',
  151. ),
  152. 'result' => array(
  153. 'ids' => array(4),
  154. ),
  155. ),
  156. // Test dataset #16
  157. 15 => array(
  158. 'cnd' => array(
  159. 'address_pattern' => 'est str',
  160. ),
  161. 'result' => array(
  162. 'ids' => array(7),
  163. ),
  164. ),
  165. // Test dataset #17
  166. 16 => array(
  167. 'cnd' => array(
  168. 'address_pattern' => 'ate te',
  169. ),
  170. 'result' => array(
  171. 'ids' => array(4),
  172. ),
  173. ),
  174. // Test dataset #18
  175. 17 => array(
  176. 'cnd' => array(
  177. 'address_pattern' => '435',
  178. ),
  179. 'result' => array(
  180. 'ids' => array(4),
  181. ),
  182. ),
  183. // Test dataset #19
  184. 18 => array(
  185. 'cnd' => array(
  186. 'user_type' => 'C',
  187. ),
  188. 'result' => array(
  189. 'ids' => array(2, 3, 4, 5, 6, 8),
  190. ),
  191. ),
  192. // Test dataset #20
  193. 19 => array(
  194. 'cnd' => array(
  195. 'date_type' => 'R',
  196. 'date_period' => 'M',
  197. ),
  198. 'result' => array(
  199. 'ids' => array(4, 6, 7, 8),
  200. 'doNotCount' => true,
  201. ),
  202. ),
  203. // Test dataset #21
  204. 20 => array(
  205. 'cnd' => array(
  206. 'date_type' => 'R',
  207. 'date_period' => 'W',
  208. ),
  209. 'result' => array(
  210. 'ids' => array(4, 6, 7),
  211. 'doNotCount' => true,
  212. ),
  213. ),
  214. // Test dataset #22
  215. 21 => array(
  216. 'cnd' => array(
  217. 'date_type' => 'R',
  218. 'date_period' => 'D',
  219. ),
  220. 'result' => array(
  221. 'ids' => array(4, 6, 7),
  222. 'doNotCount' => true,
  223. ),
  224. ),
  225. // Test dataset #23
  226. 22 => array(
  227. 'cnd' => array(
  228. 'date_type' => 'R',
  229. 'date_period' => 'C',
  230. 'startDate' => 0,
  231. 'endDate' => 0,
  232. ),
  233. 'result' => array(
  234. 'ids' => array(2),
  235. ),
  236. ),
  237. // Test dataset #24
  238. 23 => array(
  239. 'cnd' => array(
  240. 'order_id' => 0,
  241. 'date_type' => 'L',
  242. 'date_period' => 'C',
  243. 'startDate' => 0,
  244. 'endDate' => 0,
  245. ),
  246. 'result' => array(
  247. 'ids' => array(1, 2, 3, 4, 5, 6, 7),
  248. ),
  249. ),
  250. // Test dataset #25
  251. 24 => array(
  252. 'cnd' => array(
  253. 'order_by' => array('p.added', 'DESC'),
  254. 'limit' => array(1, 4), // 1 - start, 4 - limit
  255. ),
  256. 'result' => array(
  257. 'ids' => array(4, 6, 7),
  258. ),
  259. ),
  260. );
  261. /**
  262. * setUp
  263. *
  264. * @return void
  265. * @access protected
  266. * @see ____func_see____
  267. * @since 3.0.0
  268. */
  269. protected function setUp()
  270. {
  271. parent::setUp();
  272. \XLite\Core\Database::getEM()->clear();
  273. $this->query(file_get_contents(__DIR__ . '/sql/profile/setup.sql'));
  274. \XLite\Core\Database::getEM()->flush();
  275. $this->testSearchData[22]['cnd']['startDate'] = date('M j, Y', time()-60*60*24*7);
  276. $this->testSearchData[22]['cnd']['endDate'] = date('M j, Y', time()-60*60*24*3);
  277. }
  278. /**
  279. * tearDown
  280. *
  281. * @return void
  282. * @access protected
  283. * @see ____func_see____
  284. * @since 3.0.0
  285. */
  286. protected function tearDown()
  287. {
  288. parent::tearDown();
  289. $this->query(file_get_contents(__DIR__ . '/sql/profile/restore.sql'));
  290. \XLite\Core\Database::getEM()->flush();
  291. }
  292. /**
  293. * testSearch
  294. *
  295. * @return void
  296. * @access public
  297. * @see ____func_see____
  298. * @since 3.0.0
  299. */
  300. public function testSearch()
  301. {
  302. foreach ($this->testSearchData as $testId => $data) {
  303. $testIdStr = sprintf(' [%d]', $testId);
  304. $cnd = new \XLite\Core\CommonCell();
  305. foreach ($data['cnd'] as $key => $value) {
  306. $cnd->$key = $value;
  307. }
  308. $searchResults = \XLite\Core\Database::getRepo('XLite\Model\Profile')->search($cnd);
  309. $this->assertTrue(is_array($searchResults), 'The result of search() must be an array' . $testIdStr);
  310. if (!empty($data['result']['isEmpty'])) {
  311. $this->assertEquals(0, count($searchResults), 'Count of search results is expected to be zero' . $testIdStr);
  312. } else {
  313. if (!isset($data['result']['doNotCount'])) {
  314. $this->assertEquals(count($data['result']['ids']), count($searchResults), 'Checking the count of search result items' . $testIdStr);
  315. }
  316. foreach ($searchResults as $profile) {
  317. $this->assertTrue($profile instanceof \XLite\Model\Profile, 'Checking that search items are profile instances' . $testIdStr);
  318. if (!isset($data['result']['doNotCount'])) {
  319. $this->assertTrue(in_array($profile->getProfileId(), $data['result']['ids']), 'Checking that correct items are found' . $testIdStr . ' (profileId: ' . $profile->getProfileId() . ')');
  320. }
  321. }
  322. }
  323. $countSearchResults = \XLite\Core\Database::getRepo('XLite\Model\Profile')->search($cnd, true);
  324. $this->assertTrue(is_int($countSearchResults), 'Wrong type returned by search(cnd, true)' . $testIdStr);
  325. $this->assertEquals(count($searchResults), $countSearchResults, 'Checking the count of search results ('.count($searchResults).')' . $testIdStr);
  326. }
  327. }
  328. /**
  329. * testFindByLogin
  330. *
  331. * @return void
  332. * @access public
  333. * @see ____func_see____
  334. * @since 3.0.0
  335. */
  336. public function testFindByLogin()
  337. {
  338. // Test #1
  339. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLogin('rnd_tester02@rrf.ru');
  340. $this->assertTrue($profile instanceof \XLite\Model\Profile, 'check that profile is an object');
  341. $this->assertEquals('rnd_tester02@rrf.ru', $profile->getLogin(), 'check the login');
  342. // Test #2
  343. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLogin('wrong login');
  344. $this->assertNull($profile, 'check that profile is null');
  345. }
  346. /**
  347. * testFindByLoginPassword
  348. *
  349. * @return void
  350. * @access public
  351. * @see ____func_see____
  352. * @since 3.0.0
  353. */
  354. public function testFindByLoginPassword()
  355. {
  356. // Test #1
  357. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLoginPassword('rnd_tester02@rrf.ru', md5('guest'));
  358. $this->assertTrue($profile instanceof \XLite\Model\Profile, 'check that profile is an object');
  359. $this->assertEquals('rnd_tester02@rrf.ru', $profile->getLogin(), 'check the login');
  360. $this->assertTrue(is_null($profile->getOrder()), 'check the order_id (0)');
  361. // Test #2
  362. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')
  363. ->findByLoginPassword('rnd_tester02@rrf.ru', md5('wrong password'));
  364. $this->assertTrue(is_null($profile), 'check that profile is null #2');
  365. // Test #3
  366. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')
  367. ->findByLoginPassword('wrong login', md5('guest'));
  368. $this->assertTrue(is_null($profile), 'check that profile is null #3');
  369. // Test #4: user is disabled
  370. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')
  371. ->findByLoginPassword('rnd_tester03@rrf.ru', md5('guest'));
  372. $this->assertTrue(is_null($profile), 'check that profile is null #4');
  373. }
  374. /**
  375. * testFindRecentAdmins
  376. *
  377. * @return void
  378. * @access public
  379. * @see ____func_see____
  380. * @since 3.0.0
  381. */
  382. public function testFindRecentAdmins()
  383. {
  384. $profiles = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findRecentAdmins();
  385. $this->assertTrue(is_array($profiles), 'Check that array is returned');
  386. $this->assertEquals(2, count($profiles), 'Check that count of array items');
  387. foreach ($profiles as $profile) {
  388. $this->assertEquals(100, $profile->getAccessLevel(), 'Check access_level');
  389. }
  390. }
  391. /**
  392. * testFindUserWithSameLogin
  393. *
  394. * @return void
  395. * @access public
  396. * @see ____func_see____
  397. * @since 3.0.0
  398. */
  399. public function testFindUserWithSameLogin()
  400. {
  401. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLogin('rnd_tester02@rrf.ru');
  402. if (!$profile) {
  403. $this->fail('Profile \'rnd_tester02@rrf.ru\' is not found');
  404. }
  405. // Test #1
  406. $profile2 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findUserWithSameLogin($profile);
  407. $this->assertNull($profile2, 'Profile not found and must be null');
  408. // Test #2
  409. $profile->setLogin('rnd_tester04@rrf.ru');
  410. $profile2 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findUserWithSameLogin($profile);
  411. $this->assertTrue($profile2 instanceof \XLite\Model\Profile, 'Profile is found and must return an object');
  412. $this->assertEquals(6, $profile2->getProfileId(), 'check profile_id');
  413. }
  414. /**
  415. * testFindCountOfAdminAccounts
  416. *
  417. * @return void
  418. * @access public
  419. * @see ____func_see____
  420. * @since 3.0.0
  421. */
  422. public function testFindCountOfAdminAccounts()
  423. {
  424. $adminsCount = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findCountOfAdminAccounts();
  425. $this->assertEquals(2, $adminsCount, 'Checking the count of administrator accounts');
  426. }
  427. }