forked from litecommerce/core
/
OptionGroup.php
482 lines (379 loc) · 13.4 KB
/
OptionGroup.php
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
<?php
// vim: set ts=4 sw=4 sts=4 et:
/**
* LiteCommerce
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to licensing@litecommerce.com so we can send you a copy immediately.
*
* @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)
* @link http://www.litecommerce.com/
* @see ____file_see____
* @since 1.0.0
*/
/**
* XLite_Tests_Module_CDev_ProductOptions_Model_OptionGroup
*
* @see ____class_see____
* @since 1.0.22
*/
class XLite_Tests_Module_CDev_ProductOptions_Model_OptionGroup extends XLite_Tests_TestCase
{
/**
* product
*
* @var mixed
* @see ____var_see____
* @since 1.0.22
*/
protected $product;
/**
* testGroup
*
* @var array
* @see ____var_see____
* @since 1.0.22
*/
protected $testGroup = array(
'name' => 'Test name',
'fullname' => 'Test full name',
'orderby' => 10,
'type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::GROUP_TYPE,
'view_type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::SELECT_VISIBLE,
'cols' => 11,
'rows' => 12,
'enabled' => true,
);
/**
* testOption
*
* @var array
* @see ____var_see____
* @since 1.0.22
*/
protected $testOption = array(
'name' => 'Test option name',
'orderby' => 11,
'enabled' => true,
);
/**
* setUp
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
protected function setUp()
{
parent::setUp();
\XLite\Core\Database::getEM()->clear();
}
/**
* testCreate
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testCreate()
{
$group = $this->getTestGroup();
$this->assertTrue(0 < $group->getGroupId(), 'check group id type');
foreach ($this->testGroup as $k => $v) {
$m = 'get' . \XLite\Core\Converter::convertToCamelCase($k);
$this->assertEquals($v, $group->$m(), 'Check ' . $k);
}
$this->assertEquals($this->getProduct()->getProductId(), $group->getProduct()->getProductId(), 'check product id');
$this->assertEquals(1, count($group->getOptions()), 'Check options count');
$options = $group->getOptions();
$option = $options[0];
foreach ($this->testOption as $k => $v) {
$m = 'get' . \XLite\Core\Converter::convertToCamelCase($k);
$this->assertEquals($v, $option->$m(), 'Check ' . $k . ' (option)');
}
}
/**
* testUpdate
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testUpdate()
{
$group = $this->getTestGroup();
$group->setType($group::TEXT_TYPE);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals($group::TEXT_TYPE, $group->getType(), 'Check new type');
$group = \XLite\Core\Database::getRepo('XLite\Module\CDev\ProductOptions\Model\OptionGroup')
->find($group->getGroupId());
$list = $group->getOptions();
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$list[] = $option;
$option->map($this->testOption);
$option->setName('o5');
\XLite\Core\Database::getEM()->persist($option);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(2, count($group->getOptions()), 'Check options count');
}
/**
* testDelete
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testDelete()
{
$group = $this->getTestGroup();
$id = $group->getGroupId();
\XLite\Core\Database::getEM()->remove($group);
\XLite\Core\Database::getEM()->flush();
$group = \XLite\Core\Database::getRepo('XLite\Module\CDev\ProductOptions\Model\OptionGroup')
->find($id);
$this->assertNull($group, 'Check removed group');
}
/**
* testMultilanguage
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testMultilanguage()
{
$group = $this->getTestGroup();
$t = new XLite\Module\CDev\ProductOptions\Model\OptionGroupTranslation();
$t->setOwner($group);
$t->setCode('de');
$t->setName('de1');
$group->addTranslations($t);
$t = new XLite\Module\CDev\ProductOptions\Model\OptionGroupTranslation();
$t->setOwner($group);
$t->setCode('ru');
$t->setName('ru1');
$group->addTranslations($t);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$list = $group->getTranslations();
$this->assertEquals(3, count($list), 'check translations length');
\XLite\Core\Database::getEM()->remove($t);
$list->removeElement($t);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$t = $group->getTranslation('de');
$this->assertNotEquals($t->getLabelId(), $group->getGroupId(), 'check translation label id');
$this->assertEquals($t->getOwner()->getGroupId(), $group->getGroupId(), 'check translation owner id (again)');
$this->assertEquals(2, count($list), 'check translations length again');
}
/**
* testSetType
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testSetType()
{
$group = $this->getTestGroup();
$this->assertTrue($group->setType($group::TEXT_TYPE), 'Check text type');
$this->assertFalse($group->setType('z'), 'Check wrong type');
$oldViewType = $group->getViewType();
$this->assertTrue($group->setType($group::GROUP_TYPE), 'Check text type');
$this->assertNotEquals($oldViewType, $group->getViewType(), 'Check view type change');
}
/**
* testSetViewType
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testSetViewType()
{
$group = $this->getTestGroup();
$this->assertTrue($group->setViewType($group::SELECT_VISIBLE), 'Check view type');
$this->assertFalse($group->setViewType('z'), 'Check wrong view type');
$this->assertFalse($group->setViewType($group::INPUT_VISIBLE), 'Check wrong view type');
$this->assertTrue($group->setType($group::TEXT_TYPE), 'Check text type');
$this->assertTrue($group->setViewType($group::INPUT_VISIBLE), 'Check view type (again)');
}
/**
* testGetDisplayName
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testGetDisplayName()
{
$group = $this->getTestGroup();
$group->setName('n1');
$group->setFullname('n2');
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals('n2', $group->getDisplayName(), 'Get display name from full name');
$group->setFullname('');
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals('n1', $group->getDisplayName(), 'Get display name from name');
}
/**
* testGetActiveOptions
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testGetActiveOptions()
{
$group = $this->getTestGroup();
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
$option->setName('o2');
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$options = $group->getActiveOptions();
$this->assertEquals(2, count($options), 'check option count');
$option->setEnabled(false);
\XLite\Core\Database::getEM()->persist($option);
\XLite\Core\Database::getEM()->flush();
$options = $group->getActiveOptions();
$this->assertEquals(1, count($options), 'check option count again');
$this->assertNotEquals($option->getOptionId(), $options[0]->getOptionId(), 'check option id');
}
/**
* testGetDefaultOption
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testGetDefaultOption()
{
$group = $this->getTestGroup();
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
$option->setName('o3');
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
foreach ($group->getOptions() as $option) {
$option->setEnabled(true);
}
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$options = $group->getActiveOptions();
$this->assertEquals($options[0]->getOptionId(), $group->getDefaultOption()->getOptionId(), 'check option id');
$this->assertEquals($options[1]->getOptionId(), $group->getDefaultOption(1)->getOptionId(), 'check option id 2');
foreach ($group->getOptions() as $option) {
\XLite\Core\Database::getEM()->remove($option);
}
$group->getOptions()->clear();
\XLite\Core\Database::getEM()->flush();
$this->assertNull($group->getDefaultOption(), 'check empty default option');
}
/**
* testGetDefaultPlainValue
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
public function testGetDefaultPlainValue()
{
$group = $this->getTestGroup();
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
$option->setName('o4');
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
foreach ($group->getOptions() as $option) {
$option->setEnabled(true);
}
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$options = $group->getActiveOptions();
$this->assertEquals($options[0]->getOptionId(), $group->getDefaultPlainValue(), 'check option id');
$group->setType($group::TEXT_TYPE);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals('', $group->getDefaultPlainValue(), 'check empty string');
$group->setType($group::GROUP_TYPE);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
foreach ($group->getOptions() as $option) {
\XLite\Core\Database::getEM()->remove($option);
}
$group->getOptions()->clear();
\XLite\Core\Database::getEM()->flush();
$this->assertNull($group->getDefaultPlainValue(), 'check empty options list');
}
/**
* getProduct
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
protected function getProduct()
{
if (!isset($this->product)) {
$list = \XLite\Core\Database::getRepo('XLite\Model\Product')->findFrame(1, 1);
$this->product = array_shift($list);
foreach ($list as $p) {
$p->detach();
}
if (!$this->product) {
$this->fail('Can not find enabled product');
}
// Clean option groups
foreach ($this->product->getOptionGroups() as $group) {
\XLite\Core\Database::getEM()->remove($group);
}
$this->product->getOptionGroups()->clear();
\XLite\Core\Database::getEM()->flush();
}
return $this->product;
}
/**
* getTestGroup
*
* @return void
* @see ____func_see____
* @since 1.0.22
*/
protected function getTestGroup()
{
$group = new XLite\Module\CDev\ProductOptions\Model\OptionGroup();
$group->setProduct($this->getProduct());
$this->getProduct()->addOptionGroups($group);
$group->map($this->testGroup);
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
return $group;
}
}