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

Language PHP Lines 477
MD5 Hash 7f7c2aba9dcadd7e73443d9808b11fac
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
<?php
// vim: set ts=4 sw=4 sts=4 et:

/**
 * XLite\Model\Repo\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: 5f9d7fa3bf9f6754343b00cae8a3a038ae3e2417 $
 * @link       http://www.litecommerce.com/
 * @see        ____file_see____
 * @since      3.0.0
 */

class XLite_Tests_Model_Repo_Profile extends XLite_Tests_TestCase
{
    protected $testSearchData = array(
        // Test dataset #1
        0 => array(
            'cnd' => array(
                'profile_id' => 4,
            ),
            'result' => array(
                'ids'   => array(4),
            ),
        ),
        // Test dataset #2
        1 => array(
            'cnd' => array(
                'order_id' => 1,
            ),
            'result' => array(
                'ids'   => array(8),
            ),
        ),
        // Test dataset #3
        2 => array(
            'cnd' => array(
                'referer' => 'google',
            ),
            'result' => array(
                'ids'   => array(2, 3),
            ),
        ),
        // Test dataset #4
        3 => array(
            'cnd' => array(
                'membership' => 'pending_membership',
            ),
            'result' => array(
                'ids' => array(3, 5),
            ),
        ),
        // Test dataset #5
        4 => array(
            'cnd' => array(
                'membership' => 2,
            ),
            'result' => array(
                'ids' => array(4, 5),
            ),
        ),
        // Test dataset #6
        5 => array(
            'cnd' => array(
                'membership' => 1,
            ),
            'result' => array(
                'isEmpty' => true
            ),
        ),
        // Test dataset #7
        6 => array(
            'cnd' => array(
                'membership' => 2,
                'user_type'  => 'A',
            ),
            'result' => array(
                'ids'   => array(1, 7),
            ),
        ),
        // Test dataset #8
        7 => array(
            'cnd' => array(
                'language' => 'de',
            ),
            'result' => array(
                'ids' => array(4),
            ),
        ),
        // Test dataset #9
        8 => array(
            'cnd' => array(
                'pattern' => 'Patrick Smith',
            ),
            'result' => array(
                'ids' => array(3, 4),
            ),
        ),
        // Test dataset #10
        9 => array(
            'cnd' => array(
                'pattern' => 'John Smith',
            ),
            'result' => array(
                'ids' => array(2),
            ),
        ),
        // Test dataset #11
        10 => array(
            'cnd' => array(
                'pattern' => 'Smith John',
            ),
            'result' => array(
                'ids' => array(2, 3),
            ),
        ),
        // Test dataset #12
        11 => array(
            'cnd' => array(
                'phone' => '76543',
            ),
            'result' => array(
                'ids' => array(2, 3, 4, 5, 6, 7),
            ),
        ),
        // Test dataset #13
        12 => array(
            'cnd' => array(
                'country' => 'FR',
            ),
            'result' => array(
                'ids' => array(4),
            ),
        ),
        // Test dataset #14
        13 => array(
            'cnd' => array(
                'state' => 37,
            ),
            'result' => array(
                'ids' => array(5),
            ),
        ),
        // Test dataset #15
        14 => array(
            'cnd' => array(
                'address_pattern' => 'Paris',
            ),
            'result' => array(
                'ids' => array(4),
            ),
        ),
        // Test dataset #16
        15 => array(
            'cnd' => array(
                'address_pattern' => 'est str',
            ),
            'result' => array(
                'ids' => array(7),
            ),
        ),
        // Test dataset #17
        16 => array(
            'cnd' => array(
                'address_pattern' => 'ate te',
            ),
            'result' => array(
                'ids' => array(4),
            ),
        ),
        // Test dataset #18
        17 => array(
            'cnd' => array(
                'address_pattern' => '435',
            ),
            'result' => array(
                'ids' => array(4),
            ),
        ),
        // Test dataset #19
        18 => array(
            'cnd' => array(
                'user_type' => 'C',
            ),
            'result' => array(
                'ids' => array(2, 3, 4, 5, 6, 8),
            ),
        ),
        // Test dataset #20
        19 => array(
            'cnd' => array(
                'date_type'   => 'R',
                'date_period' => 'M',
            ),
            'result' => array(
                'ids'        => array(4, 6, 7, 8),
                'doNotCount' => true,
            ),
        ),
        // Test dataset #21
        20 => array(
            'cnd' => array(
                'date_type'   => 'R',
                'date_period' => 'W',
            ),
            'result' => array(
                'ids'        => array(4, 6, 7),
                'doNotCount' => true,
            ),
        ),
        // Test dataset #22
        21 => array(
            'cnd' => array(
                'date_type'   => 'R',
                'date_period' => 'D',
            ),
            'result' => array(
                'ids'        => array(4, 6, 7),
                'doNotCount' => true,
            ),
        ),
        // Test dataset #23
        22 => array(
            'cnd' => array(
                'date_type'   => 'R',
                'date_period' => 'C',
                'startDate'   => 0,
                'endDate'     => 0,
            ),
            'result' => array(
                'ids' => array(2),
            ),
        ),
        // Test dataset #24
        23 => array(
            'cnd' => array(
                'order_id'    => 0,
                'date_type'   => 'L',
                'date_period' => 'C',
                'startDate'   => 0,
                'endDate'     => 0,
            ),
            'result' => array(
                'ids' => array(1, 2, 3, 4, 5, 6, 7),
            ),
        ),
        // Test dataset #25
        24 => array(
            'cnd' => array(
                'order_by' => array('p.added', 'DESC'),
                'limit'    => array(1, 4), // 1 - start, 4 - limit
            ),
            'result' => array(
                'ids' => array(4, 6, 7),
            ),
        ),
    );
        
    /**
     * setUp
     *
     * @return void
     * @access protected
     * @see    ____func_see____
     * @since  3.0.0
     */
    protected function setUp()
    {
        parent::setUp();

        \XLite\Core\Database::getEM()->clear();

        $this->query(file_get_contents(__DIR__ . '/sql/profile/setup.sql'));
        \XLite\Core\Database::getEM()->flush();

        $this->testSearchData[22]['cnd']['startDate'] = date('M j, Y', time()-60*60*24*7);
        $this->testSearchData[22]['cnd']['endDate'] = date('M j, Y', time()-60*60*24*3);
    }

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

        $this->query(file_get_contents(__DIR__ . '/sql/profile/restore.sql'));
        \XLite\Core\Database::getEM()->flush();
    }

    /**
     * testSearch 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testSearch()
    {
        foreach ($this->testSearchData as $testId => $data) {

            $testIdStr = sprintf(' [%d]', $testId);

            $cnd = new \XLite\Core\CommonCell();

            foreach ($data['cnd'] as $key => $value) {
                $cnd->$key = $value;
            }

            $searchResults = \XLite\Core\Database::getRepo('XLite\Model\Profile')->search($cnd);

            $this->assertTrue(is_array($searchResults), 'The result of search() must be an array' . $testIdStr);

            if (!empty($data['result']['isEmpty'])) {
                $this->assertEquals(0, count($searchResults), 'Count of search results is expected to be zero' . $testIdStr);
            
            } else {

                if (!isset($data['result']['doNotCount'])) {
                    $this->assertEquals(count($data['result']['ids']), count($searchResults), 'Checking the count of search result items' . $testIdStr);
                }

                foreach ($searchResults as $profile) {
                    
                    $this->assertTrue($profile instanceof \XLite\Model\Profile, 'Checking that search items are profile instances' . $testIdStr);
                    
                    if (!isset($data['result']['doNotCount'])) {
                        $this->assertTrue(in_array($profile->getProfileId(), $data['result']['ids']), 'Checking that correct items are found' . $testIdStr . ' (profileId: ' . $profile->getProfileId() . ')');
                    }
                }
            }

            $countSearchResults = \XLite\Core\Database::getRepo('XLite\Model\Profile')->search($cnd, true);

            $this->assertTrue(is_int($countSearchResults), 'Wrong type returned by search(cnd, true)' . $testIdStr);
            $this->assertEquals(count($searchResults), $countSearchResults, 'Checking the count of search results ('.count($searchResults).')' . $testIdStr);
        }
    }

    /**
     * testFindByLogin 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testFindByLogin()
    {
        // Test #1
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLogin('rnd_tester02@rrf.ru');

        $this->assertTrue($profile instanceof \XLite\Model\Profile, 'check that profile is an object');

        $this->assertEquals('rnd_tester02@rrf.ru', $profile->getLogin(), 'check the login');

        // Test #2
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLogin('wrong login');

        $this->assertNull($profile, 'check that profile is null');
    }

    /**
     * testFindByLoginPassword 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testFindByLoginPassword()
    {
        // Test #1
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLoginPassword('rnd_tester02@rrf.ru', md5('guest'));

        $this->assertTrue($profile instanceof \XLite\Model\Profile, 'check that profile is an object');

        $this->assertEquals('rnd_tester02@rrf.ru', $profile->getLogin(), 'check the login');
        $this->assertTrue(is_null($profile->getOrder()), 'check the order_id (0)');

        // Test #2
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')
            ->findByLoginPassword('rnd_tester02@rrf.ru', md5('wrong password'));

        $this->assertTrue(is_null($profile), 'check that profile is null #2');

        // Test #3
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')
            ->findByLoginPassword('wrong login', md5('guest'));

        $this->assertTrue(is_null($profile), 'check that profile is null #3');

        // Test #4: user is disabled
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')
            ->findByLoginPassword('rnd_tester03@rrf.ru', md5('guest'));

        $this->assertTrue(is_null($profile), 'check that profile is null #4');
    }

    /**
     * testFindRecentAdmins 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testFindRecentAdmins()
    {
        $profiles = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findRecentAdmins();
    
        $this->assertTrue(is_array($profiles), 'Check that array is returned');
        $this->assertEquals(2, count($profiles), 'Check that count of array items');

        foreach ($profiles as $profile) {
            $this->assertEquals(100, $profile->getAccessLevel(), 'Check access_level');
        }
    }

    /**
     * testFindUserWithSameLogin 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testFindUserWithSameLogin()
    {
        $profile = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findByLogin('rnd_tester02@rrf.ru');

        if (!$profile) {
            $this->fail('Profile \'rnd_tester02@rrf.ru\' is not found');
        }

        // Test #1
        $profile2 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findUserWithSameLogin($profile);

        $this->assertNull($profile2, 'Profile not found and must be null');

        // Test #2
        $profile->setLogin('rnd_tester04@rrf.ru');

        $profile2 = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findUserWithSameLogin($profile);

        $this->assertTrue($profile2 instanceof \XLite\Model\Profile, 'Profile is found and must return an object');
        $this->assertEquals(6, $profile2->getProfileId(), 'check profile_id');
    }

    /**
     * testFindCountOfAdminAccounts 
     * 
     * @return void
     * @access public
     * @see    ____func_see____
     * @since  3.0.0
     */
    public function testFindCountOfAdminAccounts()
    {
        $adminsCount = \XLite\Core\Database::getRepo('XLite\Model\Profile')->findCountOfAdminAccounts();
 
        $this->assertEquals(2, $adminsCount, 'Checking the count of administrator accounts');
    }

}
Back to Top