PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/istran/core
PHP | 522 lines | 265 code | 100 blank | 157 comment | 6 complexity | c390dc0fe5950dc0a066cdd193b73f92 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\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: dde624e7abe4313cfcdb529d716bc92877b0f4d0 $
  13. * @link http://www.litecommerce.com/
  14. * @see ____file_see____
  15. * @since 3.0.0
  16. */
  17. class XLite_Tests_Model_Profile extends XLite_Tests_TestCase
  18. {
  19. /**
  20. * testProfileData
  21. *
  22. * @var array
  23. * @access protected
  24. * @see ____var_see____
  25. * @since 3.0.0
  26. */
  27. protected $testProfileData = array(
  28. // Admin profile
  29. 0 => array(
  30. 'login' => 'rnd_tester01@cdev.ru',
  31. 'password' => 'testpassword',
  32. 'access_level' => 100,
  33. 'referer' => 'some referer',
  34. ),
  35. // Customer profile
  36. 1 => array(
  37. 'login' => 'rnd_tester03@cdev.ru',
  38. 'password' => 'testpassword',
  39. 'access_level' => 0,
  40. 'referer' => 'some referer',
  41. ),
  42. // Customer profile related to some order
  43. 2 => array(
  44. 'login' => 'rnd_tester02@cdev.ru',
  45. 'password' => 'testpassword',
  46. 'access_level' => 0,
  47. 'referer' => 'some referer',
  48. ),
  49. );
  50. /**
  51. * addresses
  52. *
  53. * @var array
  54. * @access protected
  55. * @see ____var_see____
  56. * @since 3.0.0
  57. */
  58. protected $testAddresses = array(
  59. // Addresses set #0
  60. 0 => array(
  61. 0 => array(
  62. 'is_billing' => 1,
  63. 'is_shipping' => 1,
  64. 'firstname' => 'a0',
  65. ),
  66. 1 => array(
  67. 'firstname' => 'a1',
  68. ),
  69. 2 => array(
  70. 'firstname' => 'a2',
  71. ),
  72. ),
  73. // Addresses set #1
  74. 1 => array(
  75. 0 => array(
  76. 'is_billing' => 1,
  77. 'firstname' => 'a0',
  78. ),
  79. 1 => array(
  80. 'is_shipping' => 1,
  81. 'firstname' => 'a1',
  82. ),
  83. 2 => array(
  84. 'firstname' => 'a2',
  85. ),
  86. ),
  87. // Addresses set #2
  88. 2 => array(
  89. 0 => array(
  90. 'firstname' => 'a0',
  91. ),
  92. 1 => array(
  93. 'firstname' => 'a1',
  94. ),
  95. 2 => array(
  96. 'firstname' => 'a2',
  97. ),
  98. ),
  99. // Address set #3
  100. 3 => array(),
  101. // Addresses set #4
  102. 4 => array(
  103. 0 => array(
  104. 'is_billing' => 1,
  105. 'firstname' => 'a0',
  106. ),
  107. 1 => array(
  108. 'is_shipping' => 1,
  109. 'firstname' => 'a0',
  110. ),
  111. ),
  112. );
  113. /**
  114. * profileFields
  115. *
  116. * @var array
  117. * @access protected
  118. * @see ____var_see____
  119. * @since 3.0.0
  120. */
  121. protected $profileFields = array(
  122. 'login' => 'aaa',
  123. 'password' => 'password test',
  124. 'password_hint' => 'password_hint test',
  125. 'password_hint_answer' => 'password_hint_answer test',
  126. 'access_level' => 23,
  127. 'cms_profile_id' => 66666,
  128. 'cms_name' => 'cms name test',
  129. 'added' => 77777,
  130. 'first_login' => 88888,
  131. 'last_login' => 99999,
  132. 'status' => 'T',
  133. 'referer' => 'referer test',
  134. 'language' => 'ru',
  135. );
  136. /**
  137. * tearDown
  138. *
  139. * @return void
  140. * @access protected
  141. * @see ____func_see____
  142. * @since 3.0.0
  143. */
  144. protected function tearDown()
  145. {
  146. parent::tearDown();
  147. $this->query(file_get_contents(__DIR__ . '/Repo/sql/profile/restore.sql'));
  148. }
  149. /**
  150. * testGetBillingAddress
  151. *
  152. * @return void
  153. * @access public
  154. * @see ____func_see____
  155. * @since 3.0.0
  156. */
  157. public function testGetBillingAddress()
  158. {
  159. // Test #1
  160. $profile = $this->getTestProfile(0, 0);
  161. $address = $profile->getBillingAddress();
  162. $this->assertEquals('a0', $address->getFirstname(), 'Wrong billing address selected (set #0)');
  163. $this->deleteTestProfile($profile->getProfileId());
  164. // Test #2
  165. $profile = $this->getTestProfile(0, 1);
  166. $address = $profile->getBillingAddress();
  167. $this->assertEquals('a0', $address->getFirstname(), 'Wrong billing address selected (set #1)');
  168. $this->deleteTestProfile($profile->getProfileId());
  169. // Test #3
  170. $profile = $this->getTestProfile(0, 2);
  171. $address = $profile->getBillingAddress();
  172. $this->assertNull($address, 'Check that address is null (0,2)');
  173. $this->deleteTestProfile($profile->getProfileId());
  174. // Test #4
  175. $profile = $this->getTestProfile(0, 3);
  176. $address = $profile->getBillingAddress();
  177. $this->assertNull($address, 'Wrong billing address selected (set #3)');
  178. }
  179. /**
  180. * testGetShippingAddress
  181. *
  182. * @return void
  183. * @access public
  184. * @see ____func_see____
  185. * @since 3.0.0
  186. */
  187. public function testGetShippingAddress()
  188. {
  189. // Test #1
  190. $profile = $this->getTestProfile(0, 0);
  191. $address = $profile->getShippingAddress();
  192. $this->assertEquals('a0', $address->getFirstname(), 'Wrong shipping address selected (set #0)');
  193. $this->deleteTestProfile($profile->getProfileId());
  194. // Test #2
  195. $profile = $this->getTestProfile(0, 1);
  196. $address = $profile->getShippingAddress();
  197. $this->assertEquals('a1', $address->getFirstname(), 'Wrong billing address selected (set #1)');
  198. $this->deleteTestProfile($profile->getProfileId());
  199. // Test #3
  200. $profile = $this->getTestProfile(0, 2);
  201. $address = $profile->getShippingAddress();
  202. $this->assertNull($address, 'Check that address is null (0,2)');
  203. $this->deleteTestProfile($profile->getProfileId());
  204. // Test #4
  205. $profile = $this->getTestProfile(0, 3);
  206. $address = $profile->getShippingAddress();
  207. $this->assertNull($address, 'Wrong billing address selected (set #3)');
  208. }
  209. /**
  210. * testGetOrdersCount
  211. * TODO: add more tests
  212. *
  213. * @return void
  214. * @access public
  215. * @see ____func_see____
  216. * @since 3.0.0
  217. */
  218. public function testGetOrdersCount()
  219. {
  220. // Test #1
  221. $profile = $this->getTestProfile(0, 1);
  222. $this->assertEquals(0, $profile->getOrdersCount(), 'orders_count checking');
  223. }
  224. /**
  225. * testIsEnabled
  226. *
  227. * @return void
  228. * @access public
  229. * @see ____func_see____
  230. * @since 3.0.0
  231. */
  232. public function testIsEnabled()
  233. {
  234. $profile = $this->getTestProfile(0, 1);
  235. $profile->enable();
  236. $this->assertTrue($profile->isEnabled(), 'Expected status value (enabled) does not match');
  237. $profile->disable();
  238. $this->assertFalse($profile->isEnabled(), 'Expected status value (disabled) does not match');
  239. }
  240. /**
  241. * testCreate
  242. *
  243. * @return void
  244. * @access public
  245. * @see ____func_see____
  246. * @since 3.0.0
  247. */
  248. public function testCreate()
  249. {
  250. $profile = $this->getTestProfile(1, 0);
  251. foreach ($this->testProfileData[1] as $key => $value) {
  252. $methodName = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($key);
  253. $this->assertEquals($value, $profile->$methodName(), 'Wrong property (' . $key . ')' );
  254. }
  255. $this->assertTrue($profile->getMembership() instanceof \XLite\Model\Membership, 'Membership is expected to be an object');
  256. foreach ($this->profileFields as $field => $testValue) {
  257. $setterMethod = 'set' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
  258. $getterMethod = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
  259. $profile->$setterMethod($testValue);
  260. $value = $profile->$getterMethod();
  261. $this->assertEquals($testValue, $value, 'Creation checking ('.$field.')');
  262. }
  263. $this->assertTrue($profile->getPendingMembership() instanceof \XLite\Model\Membership, 'Pending membership is expected to be an object');
  264. }
  265. /**
  266. * testUpdate
  267. *
  268. * @return void
  269. * @access public
  270. * @see ____func_see____
  271. * @since 3.0.0
  272. */
  273. public function testUpdate()
  274. {
  275. // Test #1
  276. $profile1 = $this->getTestProfile(1, 0);
  277. $profile1->map($this->testProfileData[2]);
  278. $profile1->setMembership(null);
  279. $profile1->setPendingMembership(null);
  280. $result = $profile1->update();
  281. // Update result must be true
  282. $this->assertTrue($result, 'update() must return true');
  283. // Get updated profile from the database
  284. $profile2 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->find($profile1->getProfileId());
  285. // Check if profile properties are correctly updated
  286. foreach ($this->testProfileData[2] as $key => $value) {
  287. $methodName = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($key);
  288. // membership_id must be null after updating if it has a zero value initially
  289. if (in_array($key, array('membership_id', 'pending_membership_id')) && 0 === $value) {
  290. $this->assertNull($profile2->$methodName(), 'Wrong property (' . $key . ')' );
  291. } else {
  292. $this->assertEquals($value, $profile2->$methodName(), 'Wrong property (' . $key . ')' );
  293. }
  294. }
  295. $this->assertTrue(is_null($profile2->getMembership()), 'Membership is expected to be null');
  296. // Test #2: update user with login that is used by other user, check for duplicate login
  297. $profile3 = $this->getTestProfile(0, 0);
  298. $profile4 = $this->getTestProfile(1, 0);
  299. $origLogin = $profile4->getLogin();
  300. $origProfileId = $profile4->getProfileId();
  301. $profile4->setLogin($profile3->getLogin());
  302. $result = $profile4->update();
  303. $this->assertFalse($result, 'update() must return false');
  304. $profile5 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->find($origProfileId);
  305. // TODO: check why this test failed
  306. //$this->assertEquals($origLogin, $profile5->getLogin(), 'Checking for duplicate login');
  307. }
  308. /**
  309. * testIsSameAddress
  310. *
  311. * @return void
  312. * @access public
  313. * @see ____func_see____
  314. * @since 3.0.0
  315. */
  316. public function testIsSameAddress()
  317. {
  318. $profile = $this->getTestProfile(1, 1);
  319. $this->assertFalse($profile->isSameAddress(), 'isSameAddress() expected to be false');
  320. $this->deleteTestProfile($profile->getProfileId());
  321. $profile = $this->getTestProfile(1, 4);
  322. $this->assertTrue($profile->isSameAddress(), 'isSameAddress() expected to be true');
  323. }
  324. public function testIsEqualAddress()
  325. {
  326. $profile = $this->getTestProfile(1, 1);
  327. $this->assertFalse($profile->isEqualAddress(), 'isEqualAddress() expected to be false');
  328. $this->deleteTestProfile($profile->getProfileId());
  329. $profile = $this->getTestProfile(1, 4);
  330. $this->assertFalse($profile->isEqualAddress(), 'isEqualAddress() expected to be false too');
  331. $profile->getAddresses()->get(0)->setIsShipping(true);
  332. $profile->getAddresses()->removeElement($profile->getAddresses()->get(1));
  333. $this->assertTrue($profile->isEqualAddress(), 'isEqualAddress() expected to be true');
  334. }
  335. /**
  336. * testCloneObject
  337. *
  338. * @return void
  339. * @access public
  340. * @see ____func_see____
  341. * @since 3.0.0
  342. */
  343. public function testCloneEntity()
  344. {
  345. $profile = $this->getTestProfile(1, 1);
  346. $clonedProfile = $profile->cloneEntity();
  347. $this->assertTrue($clonedProfile instanceof \XLite\Model\Profile, 'Cloned profile expected to be an object');
  348. $this->assertTrue(0 < $clonedProfile->getProfileId(), 'profile_id validation');
  349. $this->assertNotEquals($profile->getProfileId(), $clonedProfile->getProfileId(), 'profile_id comparison');
  350. $this->assertEquals($profile->getLogin(), $clonedProfile->getLogin(), 'login comparison');
  351. $this->assertEquals($profile->getPassword(), $clonedProfile->getPassword(), 'password comparison');
  352. $this->assertEquals($profile->getAccessLevel(), $clonedProfile->getAccessLevel(), 'access_level comparison');
  353. $this->assertEquals($profile->getCmsProfileId(), $clonedProfile->getCmsProfileId(), 'cms_profile_id comparison');
  354. $this->assertEquals($profile->getAdded(), $clonedProfile->getAdded(), 'added comparison');
  355. $this->assertEquals($profile->getLastLogin(), $clonedProfile->getLastLogin(), 'last_login comparison');
  356. $this->assertEquals($profile->getStatus(), $clonedProfile->getStatus(), 'status comparison');
  357. $this->assertEquals($profile->getReferer(), $clonedProfile->getReferer(), 'referer comparison');
  358. $this->assertEquals($profile->getMembership()->getMembershipId(), $clonedProfile->getMembership()->getMembershipId(), 'membership_id comparison');
  359. $this->assertEquals($profile->getPendingMembership()->getMembershipId(), $clonedProfile->getPendingMembership()->getMembershipId(), 'pending_membership_id comparison');
  360. $this->assertEquals($profile->getOrder(), $clonedProfile->getOrder(), 'order_id comparison');
  361. $this->assertEquals($profile->getLanguage(), $clonedProfile->getLanguage(), 'language comparison');
  362. $membership1 = $profile->getMembership();
  363. $membership2 = $clonedProfile->getMembership();
  364. $this->assertEquals($membership1->getMembershipId(), $membership2->getMembershipId(), 'Memberships comparison');
  365. $addresses1 = $profile->getAddresses();
  366. $addresses2 = $clonedProfile->getAddresses();
  367. $this->assertLessThanOrEqual(2, count($addresses2), 'count of cloned addresses must not exceed 2');
  368. $address1 = $profile->getBillingAddress();
  369. $address2 = $clonedProfile->getBillingAddress();
  370. $this->assertNotEquals($address1->getAddressId(), $address2->getAddressId(), 'address_id comparison');
  371. $this->assertNotEquals($address1->getProfile()->getProfileId(), $address2->getProfile()->getProfileId(), 'address\'s profile_id comparison');
  372. $this->assertEquals($address1->getIsBilling(), $address2->getIsBilling(), 'is_billing comparison');
  373. $this->assertEquals($address1->getIsShipping(), $address2->getIsShipping(), 'is_shipping comparison');
  374. $this->assertEquals($address1->getFirstname(), $address2->getFirstname(), 'firstname comparison');
  375. }
  376. /**
  377. * getTestProfile
  378. *
  379. * @param int $id ____param_comment____
  380. *
  381. * @return void
  382. * @access protected
  383. * @see ____func_see____
  384. * @since 3.0.0
  385. */
  386. protected function getTestProfile($selectedProfileId = 0, $selectedAddressesId = 0)
  387. {
  388. $profile = new \XLite\Model\Profile();
  389. $profile->map($this->testProfileData[$selectedProfileId]);
  390. if (1 == $selectedProfileId) {
  391. $m = \XLite\Core\Database::getRepo('XLite\Model\Membership')->find(1);
  392. $profile->setMembership($m);
  393. $profile->setPendingMembership($m);
  394. }
  395. foreach ($this->testAddresses[$selectedAddressesId] as $data) {
  396. $address = new \XLite\Model\Address();
  397. $address->map($data);
  398. $address->setProfile($profile);
  399. $profile->addAddresses($address);
  400. }
  401. $result = $profile->create();
  402. $this->assertNotNull($profile, sprintf('Profile creation failed (%d, %d)', $selectedProfileId, $selectedAddressesId));
  403. return $result ? $profile : null;
  404. }
  405. /**
  406. * deleteTestProfile
  407. *
  408. * @param mixed $profileId ____param_comment____
  409. *
  410. * @return void
  411. * @access protected
  412. * @see ____func_see____
  413. * @since 3.0.0
  414. */
  415. protected function deleteTestProfile($profileId)
  416. {
  417. $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->find($profileId);
  418. if (isset($profile)) {
  419. $profile->delete();
  420. }
  421. }
  422. }