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

Language PHP Lines 523
MD5 Hash c390dc0fe5950dc0a066cdd193b73f92
Repository https://github.com/istran/core.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
// vim: set ts=4 sw=4 sts=4 et:

/**
 * XLite\Model\Profile class tests
 *
 * @category   LiteCommerce
 * @package    Tests
 * @subpackage Classes
 * @author     Creative Development LLC <info@cdev.ru>
 * @copyright  Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @version    GIT: $Id: dde624e7abe4313cfcdb529d716bc92877b0f4d0 $
 * @link       http://www.litecommerce.com/
 * @see        ____file_see____
 * @since      3.0.0
 */

class XLite_Tests_Model_Profile extends XLite_Tests_TestCase
{
    /**
     * testProfileData 
     * 
     * @var    array
     * @access protected
     * @see    ____var_see____
     * @since  3.0.0
     */
    protected $testProfileData = array(
        // Admin profile
        0 => array(
            'login'         => 'rnd_tester01@cdev.ru',
            'password'      => 'testpassword',
            'access_level'  => 100,
            'referer'       => 'some referer',
        ),
        // Customer profile
        1 => array(
            'login'         => 'rnd_tester03@cdev.ru',
            'password'      => 'testpassword',
            'access_level'  => 0,
            'referer'       => 'some referer',
        ),
        // Customer profile related to some order
        2 => array(
            'login'         => 'rnd_tester02@cdev.ru',
            'password'      => 'testpassword',
            'access_level'  => 0,
            'referer'       => 'some referer',
        ),
    );

    /**
     * addresses 
     * 
     * @var    array
     * @access protected
     * @see    ____var_see____
     * @since  3.0.0
     */
    protected $testAddresses = array(
        // Addresses set #0
        0 => array(
            0 => array(
                'is_billing'  => 1,
                'is_shipping' => 1,
                'firstname'   => 'a0',
            ),
            1 => array(
                'firstname'   => 'a1',
            ),
            2 => array(
                'firstname'   => 'a2',
            ),
        ),
        // Addresses set #1
        1 => array(
            0 => array(
                'is_billing'   => 1,
                'firstname'   => 'a0',
            ),
            1 => array(
                'is_shipping'  => 1,
                'firstname'   => 'a1',
            ),
            2 => array(
                'firstname'   => 'a2',
            ),
        ),
        // Addresses set #2
        2 => array(
            0 => array(
                'firstname'   => 'a0',
            ),
            1 => array(
                'firstname'   => 'a1',
            ),
            2 => array(
                'firstname'   => 'a2',
            ),
        ),
        // Address set #3
        3 => array(),
        // Addresses set #4
        4 => array(
            0 => array(
                'is_billing'   => 1,
                'firstname'   => 'a0',
            ),
            1 => array(
                'is_shipping'  => 1,
                'firstname'   => 'a0',
            ),
        ),

    );

    /**
     * profileFields 
     * 
     * @var    array
     * @access protected
     * @see    ____var_see____
     * @since  3.0.0
     */
    protected $profileFields = array(
        'login'                 => 'aaa',
        'password'              => 'password test',
        'password_hint'         => 'password_hint test',
        'password_hint_answer'  => 'password_hint_answer test',
        'access_level'          => 23,
        'cms_profile_id'        => 66666,
        'cms_name'              => 'cms name test',
        'added'                 => 77777,
        'first_login'           => 88888,
        'last_login'            => 99999,
        'status'                => 'T',
        'referer'               => 'referer test',
        'language'              => 'ru',
    );

    /**
     * tearDown
     *
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function tearDown()
    {
        parent::tearDown();

        $this->query(file_get_contents(__DIR__ . '/Repo/sql/profile/restore.sql'));
    }

    /**
     * testGetBillingAddress 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testGetBillingAddress()
    {
        // Test #1
        $profile = $this->getTestProfile(0, 0);

        $address = $profile->getBillingAddress();

        $this->assertEquals('a0', $address->getFirstname(), 'Wrong billing address selected (set #0)');

        $this->deleteTestProfile($profile->getProfileId());

        // Test #2
        $profile = $this->getTestProfile(0, 1);

        $address = $profile->getBillingAddress();

        $this->assertEquals('a0', $address->getFirstname(), 'Wrong billing address selected (set #1)');

        $this->deleteTestProfile($profile->getProfileId());

        // Test #3
        $profile = $this->getTestProfile(0, 2);

        $address = $profile->getBillingAddress();

        $this->assertNull($address, 'Check that address is null (0,2)');

        $this->deleteTestProfile($profile->getProfileId());

        // Test #4
        $profile = $this->getTestProfile(0, 3);

        $address = $profile->getBillingAddress();

        $this->assertNull($address, 'Wrong billing address selected (set #3)');

    }

    /**
     * testGetShippingAddress 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testGetShippingAddress()
    {
        // Test #1
        $profile = $this->getTestProfile(0, 0);

        $address = $profile->getShippingAddress();

        $this->assertEquals('a0', $address->getFirstname(), 'Wrong shipping address selected (set #0)');

        $this->deleteTestProfile($profile->getProfileId());

        // Test #2
        $profile = $this->getTestProfile(0, 1);

        $address = $profile->getShippingAddress();

        $this->assertEquals('a1', $address->getFirstname(), 'Wrong billing address selected (set #1)');

        $this->deleteTestProfile($profile->getProfileId());

        // Test #3
        $profile = $this->getTestProfile(0, 2);

        $address = $profile->getShippingAddress();

        $this->assertNull($address, 'Check that address is null (0,2)');

        $this->deleteTestProfile($profile->getProfileId());

        // Test #4
        $profile = $this->getTestProfile(0, 3);

        $address = $profile->getShippingAddress();

        $this->assertNull($address, 'Wrong billing address selected (set #3)');

    }

    /**
     * testGetOrdersCount 
     * TODO: add more tests
     *
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testGetOrdersCount()
    {
        // Test #1
        $profile = $this->getTestProfile(0, 1);

        $this->assertEquals(0, $profile->getOrdersCount(), 'orders_count checking');
    }

    /**
     * testIsEnabled 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testIsEnabled()
    {
        $profile = $this->getTestProfile(0, 1);

        $profile->enable();

        $this->assertTrue($profile->isEnabled(), 'Expected status value (enabled) does not match');

        $profile->disable();

        $this->assertFalse($profile->isEnabled(), 'Expected status value (disabled) does not match');
    }

    /**
     * testCreate 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testCreate()
    {
        $profile = $this->getTestProfile(1, 0);

        foreach ($this->testProfileData[1] as $key => $value) {
            $methodName = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($key);
            $this->assertEquals($value, $profile->$methodName(), 'Wrong property (' . $key . ')' );
        }

        $this->assertTrue($profile->getMembership() instanceof \XLite\Model\Membership, 'Membership is expected to be an object');

        foreach ($this->profileFields as $field => $testValue) {
            $setterMethod = 'set' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
            $getterMethod = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field);
            $profile->$setterMethod($testValue);
            $value = $profile->$getterMethod();
            $this->assertEquals($testValue, $value, 'Creation checking ('.$field.')');
        }

        $this->assertTrue($profile->getPendingMembership() instanceof \XLite\Model\Membership, 'Pending membership is expected to be an object');
    }

    /**
     * testUpdate 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testUpdate()
    {
        // Test #1
        $profile1 = $this->getTestProfile(1, 0);

        $profile1->map($this->testProfileData[2]);
        $profile1->setMembership(null);
        $profile1->setPendingMembership(null);

        $result = $profile1->update();
        
        // Update result must be true
        $this->assertTrue($result, 'update() must return true');

        // Get updated profile from the database
        $profile2 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->find($profile1->getProfileId());

        // Check if profile properties are correctly updated
        foreach ($this->testProfileData[2] as $key => $value) {
            $methodName = 'get' . \XLite\Core\Converter::getInstance()->convertToCamelCase($key);
            // membership_id must be null after updating if it has a zero value initially
            if (in_array($key, array('membership_id', 'pending_membership_id')) && 0 === $value) {
                $this->assertNull($profile2->$methodName(), 'Wrong property (' . $key . ')' );

            } else {
                $this->assertEquals($value, $profile2->$methodName(), 'Wrong property (' . $key . ')' );
            }
        }

        $this->assertTrue(is_null($profile2->getMembership()), 'Membership is expected to be null');

        // Test #2: update user with login that is used by other user, check for duplicate login
        
        $profile3 = $this->getTestProfile(0, 0);

        $profile4 = $this->getTestProfile(1, 0);

        $origLogin = $profile4->getLogin();
        $origProfileId = $profile4->getProfileId();

        $profile4->setLogin($profile3->getLogin());

        $result = $profile4->update();

        $this->assertFalse($result, 'update() must return false');

        $profile5 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->find($origProfileId);

        // TODO: check why this test failed
        //$this->assertEquals($origLogin, $profile5->getLogin(), 'Checking for duplicate login');
    }

    /**
     * testIsSameAddress 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testIsSameAddress()
    {
        $profile = $this->getTestProfile(1, 1);

        $this->assertFalse($profile->isSameAddress(), 'isSameAddress() expected to be false');

        $this->deleteTestProfile($profile->getProfileId());

        $profile = $this->getTestProfile(1, 4);

        $this->assertTrue($profile->isSameAddress(), 'isSameAddress() expected to be true');
    }

    public function testIsEqualAddress()
    {
        $profile = $this->getTestProfile(1, 1);

        $this->assertFalse($profile->isEqualAddress(), 'isEqualAddress() expected to be false');

        $this->deleteTestProfile($profile->getProfileId());

        $profile = $this->getTestProfile(1, 4);

        $this->assertFalse($profile->isEqualAddress(), 'isEqualAddress() expected to be false too');

        $profile->getAddresses()->get(0)->setIsShipping(true);
        $profile->getAddresses()->removeElement($profile->getAddresses()->get(1));

        $this->assertTrue($profile->isEqualAddress(), 'isEqualAddress() expected to be true');
    }

    /**
     * testCloneObject 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testCloneEntity()
    {
        $profile = $this->getTestProfile(1, 1);

        $clonedProfile = $profile->cloneEntity();

        $this->assertTrue($clonedProfile instanceof \XLite\Model\Profile, 'Cloned profile expected to be an object');

        $this->assertTrue(0 < $clonedProfile->getProfileId(), 'profile_id validation');
        $this->assertNotEquals($profile->getProfileId(), $clonedProfile->getProfileId(), 'profile_id comparison');
        $this->assertEquals($profile->getLogin(), $clonedProfile->getLogin(), 'login comparison');
        $this->assertEquals($profile->getPassword(), $clonedProfile->getPassword(), 'password comparison');
        $this->assertEquals($profile->getAccessLevel(), $clonedProfile->getAccessLevel(), 'access_level comparison');
        $this->assertEquals($profile->getCmsProfileId(), $clonedProfile->getCmsProfileId(), 'cms_profile_id comparison');
        $this->assertEquals($profile->getAdded(), $clonedProfile->getAdded(), 'added comparison');
        $this->assertEquals($profile->getLastLogin(), $clonedProfile->getLastLogin(), 'last_login comparison');
        $this->assertEquals($profile->getStatus(), $clonedProfile->getStatus(), 'status comparison');
        $this->assertEquals($profile->getReferer(), $clonedProfile->getReferer(), 'referer comparison');
        $this->assertEquals($profile->getMembership()->getMembershipId(), $clonedProfile->getMembership()->getMembershipId(), 'membership_id comparison');
        $this->assertEquals($profile->getPendingMembership()->getMembershipId(), $clonedProfile->getPendingMembership()->getMembershipId(), 'pending_membership_id comparison');
        $this->assertEquals($profile->getOrder(), $clonedProfile->getOrder(), 'order_id comparison');
        $this->assertEquals($profile->getLanguage(), $clonedProfile->getLanguage(), 'language comparison');

        $membership1 = $profile->getMembership();
        $membership2 = $clonedProfile->getMembership();

        $this->assertEquals($membership1->getMembershipId(), $membership2->getMembershipId(), 'Memberships comparison');

        $addresses1 = $profile->getAddresses();
        $addresses2 = $clonedProfile->getAddresses();

        $this->assertLessThanOrEqual(2, count($addresses2), 'count of cloned addresses must not exceed 2');

        $address1 = $profile->getBillingAddress();
        $address2 = $clonedProfile->getBillingAddress();

        $this->assertNotEquals($address1->getAddressId(), $address2->getAddressId(), 'address_id comparison');
        $this->assertNotEquals($address1->getProfile()->getProfileId(), $address2->getProfile()->getProfileId(), 'address\'s profile_id comparison');
        $this->assertEquals($address1->getIsBilling(), $address2->getIsBilling(), 'is_billing comparison');
        $this->assertEquals($address1->getIsShipping(), $address2->getIsShipping(), 'is_shipping comparison');
        $this->assertEquals($address1->getFirstname(), $address2->getFirstname(), 'firstname comparison');
    }

    /**
     * getTestProfile 
     * 
     * @param int $id ____param_comment____
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function getTestProfile($selectedProfileId = 0, $selectedAddressesId = 0)
    {
        $profile = new \XLite\Model\Profile();

        $profile->map($this->testProfileData[$selectedProfileId]);

        if (1 == $selectedProfileId) {
            $m = \XLite\Core\Database::getRepo('XLite\Model\Membership')->find(1);
            $profile->setMembership($m);
            $profile->setPendingMembership($m);
        }

        foreach ($this->testAddresses[$selectedAddressesId] as $data) {
            $address = new \XLite\Model\Address();
            $address->map($data);
            $address->setProfile($profile);
            $profile->addAddresses($address);
        }

        $result = $profile->create();

        $this->assertNotNull($profile, sprintf('Profile creation failed (%d, %d)', $selectedProfileId, $selectedAddressesId));

        return $result ? $profile : null;
    }

    /**
     * deleteTestProfile 
     * 
     * @param mixed $profileId ____param_comment____
     *  
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function deleteTestProfile($profileId)
    {
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->find($profileId);

        if (isset($profile)) {
            $profile->delete();
        }
    }

}
Back to Top