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