PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 27ms app.codeStats 0ms

/.dev/tests/Classes/Model/Module.php

https://github.com/istran/core
PHP | 320 lines | 219 code | 63 blank | 38 comment | 2 complexity | 77ac55273aa16dd0c41bfceb1048b955 MD5 | raw file
  1<?php
  2// vim: set ts=4 sw=4 sts=4 et:
  3
  4/**
  5 * LiteCommerce
  6 * 
  7 * NOTICE OF LICENSE
  8 * 
  9 * This source file is subject to the Open Software License (OSL 3.0)
 10 * that is bundled with this package in the file LICENSE.txt.
 11 * It is also available through the world-wide-web at this URL:
 12 * http://opensource.org/licenses/osl-3.0.php
 13 * If you did not receive a copy of the license and are unable to
 14 * obtain it through the world-wide-web, please send an email
 15 * to licensing@litecommerce.com so we can send you a copy immediately.
 16 * 
 17 * @category   LiteCommerce
 18 * @package    Tests
 19 * @subpackage Classes
 20 * @author     Creative Development LLC <info@cdev.ru> 
 21 * @copyright  Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
 22 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 23 * @version    GIT: $Id: 9ed56d3a7f0e1c6d67e99f24a849d616714619fe $
 24 * @link       http://www.litecommerce.com/
 25 * @see        ____file_see____
 26 * @since      3.0.0
 27 */
 28
 29class XLite_Tests_Model_Module extends XLite_Tests_Model_ModuleAbstract
 30{
 31
 32    // TODO: implement using a test module package once installation and packing
 33    //       is complete. Currently Featured products module is used for testing
 34    
 35    public function testCreate()
 36    {
 37        $module = $this->getTestModule();
 38
 39        $this->assertTrue(0 < $module->getModuleId(), 'Check Module ID');
 40
 41        foreach ($this->testModule as $k => $v) {
 42            $m = 'get' . \XLite\Core\Converter::convertToCamelCase($k);
 43            $this->assertEquals($v, $module->$m(), 'Check ' . $k);
 44        }
 45
 46        // Test create() method itself
 47        $module = new \XLite\Model\Module();
 48        $module->create('CreatedModule', 'CreatedAuthor');
 49
 50        $module = \XLite\Core\Database::getRepo('XLite\Model\Module')->find($module->getModuleId());
 51        $this->assertEquals('CreatedModule', $module->getName(), 'Check name using create() method');
 52        $this->assertEquals('CreatedAuthor', $module->getAuthor(), 'Check author using create() method');
 53    }
 54
 55    public function testUpdate()
 56    {
 57        $newValues = array(
 58            'name'          => 'TestModule1',
 59            'author'        => 'TestAuthor1',
 60            'moduleName'    => 'Test module name1',
 61            'authorName'    => 'Test module author1',
 62            'description'   => 'Test module description1',
 63            'installed'     => false,
 64            'version'       => '1.2.31',
 65            'rating'        => 99,
 66            'downloads'     => 101,
 67            'iconURL'       => 'icon/url/test/path1',
 68            'date'          => 1295513913,
 69            'changelog'     => 'Test changelog1',
 70            'currency'      => 'USD',
 71            'purchased'     => true,
 72            'price'         => 10.12,
 73            'uploadCode'    => 'abc',
 74            'packHash'      => 'asdasd',
 75            'dependencies'  => array(
 76                array('CDev', 'AOM')
 77            ),
 78        );
 79
 80        $module = $this->getTestModule();
 81
 82        // Set values
 83        foreach ($newValues as $k => $newval) {
 84            $m = 'set' . \XLite\Core\Converter::convertToCamelCase($k);
 85            $module->$m($newval);
 86        }
 87
 88        $module->setStatus($module::EXISTS);
 89
 90        \XLite\Core\Database::getEM()->persist($module);
 91        \XLite\Core\Database::getEM()->flush();
 92
 93        \XLite\Core\Database::getEM()->clear();
 94
 95        // Get values
 96        $module = \XLite\Core\Database::getRepo('XLite\Model\Module')->find($module->getModuleId());
 97
 98        foreach ($newValues as $k => $newval) {
 99            $m = 'get' . \XLite\Core\Converter::convertToCamelCase($k);
100            $this->assertEquals($newval, $module->$m(), 'check new ' . $k);
101            $module->$m();
102        }
103
104        $this->assertEquals($module::EXISTS, $module->getStatus(), 'check new status');
105    }
106
107    public function testDelete()
108    {
109        $module = $this->getTestModule();
110
111        $id = $module->getModuleId();
112
113        \XLite\Core\Database::getEM()->remove($module);
114        \XLite\Core\Database::getEM()->flush();
115
116        $module = \XLite\Core\Database::getRepo('XLite\Model\Module')
117            ->find($id);
118
119        $this->assertTrue(is_null($module), 'check removed module');
120    }
121
122    public function testHasIcon()
123    {
124        $module = $this->getTestModule();
125        $this->assertTrue($module->hasIcon(), 'check if module has icon');
126    }
127
128    public function testIsFree()
129    {
130        $module = $this->getTestModule();
131        $this->assertFalse($module->isFree(), 'check if module is free');
132    }
133
134    public function testGetMarketplaceURL()
135    {
136        $module = $this->getTestModule();
137        $this->assertEquals('https://www.litecommerce.com/marketplace/', $module::getMarketplaceURL(), 'check marketplace URL');
138    }
139
140    public function testGetPageURL()
141    {
142        $module = $this->getTestModule();
143        $this->assertEquals('https://www.litecommerce.com/marketplace/module/TestAuthor/TestModule', $module->getPageURL(), 'check module URL');
144    }
145
146    public function testGetAuthorPageURL()
147    {
148        $module = $this->getTestModule();
149        $this->assertEquals('https://www.litecommerce.com/marketplace/module/TestAuthor', $module->getAuthorPageURL(), 'check module author URL');
150    }
151
152    public function testSetEnabled()
153    {
154        $module = $this->getEnabledModule();
155        $module->setEnabled(false);
156        \XLite\Core\Database::getEM()->flush();
157
158        \XLite\Core\Database::getEM()->clear();
159 
160        $module = \XLite\Core\Database::getRepo('XLite\Model\Module')->find($module->getModuleId());
161        $this->assertFalse($module->getEnabled(), 'check if module is disabled');
162        
163        $module->setEnabled(true);
164        \XLite\Core\Database::getEM()->flush();
165        $this->assertTrue($module->getEnabled(), 'check if module is enabled');
166    }
167
168    public function testProtectedStructures()
169    {
170        $path = LC_VAR_DIR . '.disabled.structures.php';
171
172        if (file_exists($path)) {
173            unlink($path);
174        }
175
176        $module = \Xlite\Core\Database::getRepo('XLite\Model\Module')
177            ->findOneBy(array('author' => 'CDev', 'name' => 'ProductOptions'));
178        $module->disableModule();
179        \XLite\Core\Database::getEM()->flush();
180
181        $etalon = "# <?php if (!defined('LC_DS')) { die(); } ?>
182
183CDev\ProductOptions:
184  tables: [options, option_exceptions, option_groups, option_group_translations, option_surcharges, option_translations, order_item_options]
185  columns: {  }
186";
187
188        $this->assertEquals($etalon, file_get_contents($path), 'check .disabled.structures.php');
189        if (file_exists($path)) {
190            unlink($path);
191        }
192    }
193
194    public function testPrepareUpdate()
195    {
196        // TODO: check if possible
197    }
198
199    public function testGetDependedModules()
200    {
201        // TODO: implement after module installation tests are done
202    }
203
204    public function testDisableModule()
205    {
206        $module = $this->getEnabledModule();
207        $module->disableModule();
208        $this->assertFalse($module->getEnabled(), 'check if module is disabled');
209    }
210
211    public function testGetSettingsFormLink()
212    {
213        $module = $this->getEnabledModule(true, 'FeaturedProducts');
214        $this->assertEquals('admin.php?target=module&moduleId=' . $module->getModuleId(), $module->getSettingsFormLink(), 'check general settings form link');
215        
216        $module = $this->getEnabledModule(true, 'AustraliaPost');
217        $this->assertEquals('admin.php?target=aupost', $module->getSettingsFormLink(), 'check custom settings form link');
218    }
219    
220    public function testGetMainClass()
221    {
222        $module = $this->getEnabledModule(true, 'FeaturedProducts');
223        $this->assertEquals('\XLite\Module\CDev\FeaturedProducts\Main', $module->getMainClass(), 'check main class');
224    }
225    
226    public function testGetDependenciesModules()
227    {
228        // TODO: implement after dependencies system refactoring
229    }
230    
231    public function testCanEnable()
232    {
233        $module = $this->getEnabledModule();
234
235        $module->setInstalled(false);
236        $this->assertFalse($module->canEnable(), 'check if module can be enabled');
237
238        // TODO: implement dependencies checker after dependencies system refactoring
239        // TODO: implement checking of the static check() method of the main class
240    }
241    
242    public function testGetHash()
243    {
244        // TODO: implement after all upload/install functions and tests are completed
245    }
246
247    public function testUninstall()
248    {
249        // TODO: after packing mechanism is completed
250    }
251
252    public function testAnonymousCall()
253    {
254        $module = $this->getEnabledModule(true, 'FeaturedProducts');
255
256        $module->setModuleName('aaa');
257        $module->setAuthorName('bbb');
258        $module->setDescription('ccc');
259
260        $this->assertEquals('This module enables featured products list', $module->__call('getDescription'), 'check description call');
261        $this->assertEquals('Featured Products', $module->__call('getModuleName'), 'check name call');
262        $this->assertEquals('Creative Development LLC', $module->__call('getAuthorName'), 'check author call');
263    }
264    
265    public function testIsUpdateAvailable()
266    {
267        $module = $this->getEnabledModule(true, 'FeaturedProducts');
268        $this->assertFalse($module->isUpdateAvailable(), 'check not available update');
269
270        $module->setVersion($module->getVersion() . '.1');
271        $this->assertTrue($module->isUpdateAvailable(), 'check available update');
272    }
273
274    public function testGetLastVersion()
275    {
276        $module = $this->getEnabledModule(true, 'FeaturedProducts');
277        $this->assertEquals($module->getVersion(), $module->getLastVersion(), 'check last version getter');
278    }
279
280    public function testGetCurrentVersion()
281    {
282        $module = $this->getEnabledModule(true, 'FeaturedProducts');
283        $oldVer = $module->getVersion();
284        $module->setVersion($module->getVersion() . '.1');
285        $this->assertEquals($oldVer, $module->getCurrentVersion(), 'check current version getter');
286    }
287
288    public function testGetActualName()
289    {
290        $module = $this->getTestModule();
291        $this->assertEquals('TestAuthor\\TestModule', $module->getActualName(), 'check actual name');
292    }
293
294    public function testGetPath()
295    {
296        $module = $this->getTestModule();
297        $this->assertEquals('TestAuthor/TestModule', $module->getPath(), 'check path');
298    }
299 
300    public function testGetModel()
301    {
302        $module = $this->getTestModule();
303        $this->assertEquals($module->getModel(), $module->model, 'check model');
304
305    }
306
307    public function testCanUpload()
308    {
309        $module = $this->getTestModule();
310        $this->assertFalse($module->canUpload(), 'check if can upload');
311
312        $module->setUploadCode('1234567890.1234567890.1234567890');
313        $this->assertTrue($module->canUpload(), 'check if can upload');
314    }
315
316    public function testUpload()
317    {
318        // TODO:
319    }
320}