Passed
Push — master ( c6e392...14ba76 )
by Aimeos
03:51
created

StandardTest::testBodyAddVariantAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 35
rs 9.552
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 */
8
9
10
namespace Aimeos\Client\Html\Basket\Standard;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $context;
17
	private $view;
18
19
20
	protected function setUp() : void
21
	{
22
		$this->context = \TestHelperHtml::context();
23
24
		$this->view = \TestHelperHtml::view();
25
		$this->view->standardBasket = \Aimeos\MShop::create( $this->context, 'order/base' )->create();
26
27
		$this->object = new \Aimeos\Client\Html\Basket\Standard\Standard( $this->context );
28
		$this->object = new \Aimeos\Client\Html\Common\Decorator\Exceptions( $this->object, $this->context );
29
		$this->object->setView( $this->view );
30
	}
31
32
33
	protected function tearDown() : void
34
	{
35
		\Aimeos\Controller\Frontend\Basket\Factory::create( $this->context )->clear();
36
		unset( $this->object, $this->context, $this->view );
37
	}
38
39
40
	public function testHeader()
41
	{
42
		$tags = [];
43
		$expire = null;
44
45
		$this->object->setView( $this->object->data( $this->view, $tags, $expire ) );
46
		$output = $this->object->header();
47
48
		$this->assertStringContainsString( '<title>Basket | Aimeos</title>', $output );
49
	}
50
51
52
	public function testBody()
53
	{
54
		$output = $this->object->body();
55
56
		$this->assertStringStartsWith( '<section class="aimeos basket-standard"', $output );
57
		$this->assertStringContainsString( '<div class="common-summary-detail', $output );
58
		$this->assertStringContainsString( '<div class="basket-standard-coupon', $output );
59
	}
60
61
62
	public function testBodyAddSingle()
63
	{
64
		$param = array(
65
			'b_action' => 'add',
66
			'b_prodid' => $this->getProductItem( 'CNE' )->getId(),
67
			'b_quantity' => 1,
68
			'b_stocktype' => 'default',
69
		);
70
71
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
72
		$this->view->addHelper( 'param', $helper );
73
74
		$this->object->init();
75
		$output = $this->object->body();
76
77
		$this->assertRegExp( '#<div class="price.+18.00 .+</div>#smU', $output );
78
		$this->assertRegExp( '#<div class="subtotal.+<div class="price.+18.00 .+</div>#smU', $output );
79
		$this->assertRegExp( '#<div class="delivery.+<div class="price.+1.00 .+</div>#smU', $output );
80
		$this->assertRegExp( '#<div class="total.+<div class="price.+19.00 .+</div>#smU', $output );
81
		$this->assertRegExp( '#<div class="tax.+<div class="price.+3.03 .+</div>#smU', $output );
82
	}
83
84
85
	public function testBodyAddMulti()
86
	{
87
		$param = array(
88
			'b_action' => 'add',
89
			'b_prod' => array(
90
				array(
91
					'prodid' => $this->getProductItem( 'CNC' )->getId(),
92
					'quantity' => 1,
93
					'stocktype' => 'default',
94
				),
95
				array(
96
					'prodid' => $this->getProductItem( 'CNE' )->getId(),
97
					'quantity' => 1,
98
					'stocktype' => 'default',
99
				),
100
			),
101
		);
102
103
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
104
		$this->view->addHelper( 'param', $helper );
105
106
		$this->object->init();
107
		$output = $this->object->body();
108
109
		$this->assertRegExp( '#<div class="price.+18.00 .+</div>#smU', $output );
110
		$this->assertRegExp( '#<div class="price.+600.00 .+</div>#smU', $output );
111
		$this->assertRegExp( '#<div class="subtotal.+<div class="price.+618.00 .+</div>#smU', $output );
112
		$this->assertRegExp( '#<div class="delivery.+<div class="price.+31.00 .+</div>#smU', $output );
113
		$this->assertRegExp( '#<div class="total.+<div class="price.+649.00 .+</div>#smU', $output );
114
	}
115
116
117
	public function testBodyAddVariantAttribute()
118
	{
119
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
120
121
		$search = $attrManager->filter();
122
		$expr = array(
123
			$search->compare( '==', 'attribute.domain', 'product' ),
124
			$search->or( array(
125
				$search->and( array(
126
					$search->compare( '==', 'attribute.code', '30' ),
127
					$search->compare( '==', 'attribute.type', 'length' ),
128
				) ),
129
				$search->and( array(
130
					$search->compare( '==', 'attribute.code', '30' ),
131
					$search->compare( '==', 'attribute.type', 'width' ),
132
				) ),
133
			) ),
134
		);
135
		$search->setConditions( $search->and( $expr ) );
136
		$attributes = $attrManager->search( $search );
137
138
		$param = array(
139
			'b_action' => 'add',
140
			'b_prodid' => $this->getProductItem( 'U:TEST' )->getId(),
141
			'b_quantity' => 2,
142
			'b_attrvarid' => $attributes->keys()->toArray(),
143
		);
144
145
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
146
		$this->view->addHelper( 'param', $helper );
147
148
		$this->object->init();
149
		$output = $this->object->body();
150
151
		$this->assertRegExp( '#<li class="attr-item.*<span class="value">.*30.*</span>.*</li>.*<li class="attr-item.*<span class="value">.*30.*</span>.*</li>#smU', $output );
152
	}
153
154
155
	public function testBodyAddConfigAttribute()
156
	{
157
		$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
158
159
		$search = $manager->filter();
160
		$expr = array(
161
			$search->compare( '==', 'attribute.code', 'white' ),
162
			$search->compare( '==', 'attribute.domain', 'product' ),
163
			$search->compare( '==', 'attribute.type', 'color' ),
164
		);
165
		$search->setConditions( $search->and( $expr ) );
166
167
		if( ( $attribute = $manager->search( $search )->first() ) === null ) {
168
			throw new \RuntimeException( 'No attribute' );
169
		}
170
171
		$param = array(
172
			'b_action' => 'add',
173
			'b_prodid' => $this->getProductItem( 'CNE' )->getId(),
174
			'b_quantity' => 2,
175
			'b_attrconfid' => ['id' => [0 => $attribute->getId()], 'qty' => [0 =>1]],
176
			'b_stocktype' => 'default',
177
		);
178
179
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
180
		$this->view->addHelper( 'param', $helper );
181
182
		$this->object->init();
183
		$output = $this->object->body();
184
185
		$this->assertRegExp( '#<li class="attr-item.*<span class="value">.*weiß.*</span>.*</li>#smU', $output );
186
	}
187
188
189
	public function testBodyAddCustomAttribute()
190
	{
191
		$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
192
193
		$search = $manager->filter();
194
		$expr = array(
195
			$search->compare( '==', 'attribute.code', 'custom' ),
196
			$search->compare( '==', 'attribute.domain', 'product' ),
197
			$search->compare( '==', 'attribute.type', 'date' ),
198
		);
199
		$search->setConditions( $search->and( $expr ) );
200
201
		if( ( $attribute = $manager->search( $search )->first() ) === null ) {
202
			throw new \RuntimeException( 'No attribute' );
203
		}
204
205
		$param = array(
206
			'b_action' => 'add',
207
			'b_prodid' => $this->getProductItem( 'U:TESTP' )->getId(),
208
			'b_quantity' => 2,
209
			'b_attrcustid' => array( $attribute->getId() => '2000-01-01' ),
210
			'b_stocktype' => 'default',
211
		);
212
213
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
214
		$this->view->addHelper( 'param', $helper );
215
216
		$this->object->init();
217
		$output = $this->object->body();
218
219
		$this->assertRegExp( '#<li class="attr-item.*<span class="value">.*2000-01-01.*</span>.*</li>#smU', $output );
220
	}
221
222
223
	public function testBodyEditSingle()
224
	{
225
		$this->addProduct( 'CNE', 2, 'default' );
226
227
		$param = array(
228
			'b_action' => 'edit',
229
			'b_position' => 0,
230
			'b_quantity' => 1,
231
		);
232
233
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
234
		$this->view->addHelper( 'param', $helper );
235
236
		$this->object->init();
237
		$output = $this->object->body();
238
239
		$this->assertRegExp( '#<div class="price.+18.00 .+</div>#smU', $output );
240
		$this->assertRegExp( '#<div class="subtotal.+<div class="price.+18.00 .+</div>#smU', $output );
241
		$this->assertRegExp( '#<div class="delivery.+<div class="price.+1.00 .+</div>#smU', $output );
242
		$this->assertRegExp( '#<div class="total.+<div class="price.+19.00 .+</div>#smU', $output );
243
	}
244
245
246
	public function testBodyEditMulti()
247
	{
248
		$this->addProduct( 'CNE', 1, 'default' );
249
		$this->addProduct( 'CNC', 2, 'default' );
250
251
		$param = array(
252
			'b_action' => 'edit',
253
			'b_prod' => array(
254
				array(
255
					'position' => 0,
256
					'quantity' => 2,
257
				),
258
				array(
259
					'position' => 1,
260
					'quantity' => 1,
261
				),
262
			),
263
		);
264
265
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
266
		$this->view->addHelper( 'param', $helper );
267
268
		$this->object->init();
269
		$output = $this->object->body();
270
271
		$this->assertRegExp( '#<div class="price.+36.00 .+</div>#smU', $output );
272
		$this->assertRegExp( '#<div class="price.+600.00 .+</div>#smU', $output );
273
		$this->assertRegExp( '#<div class="subtotal.+<div class="price.+636.00 .+</div>#smU', $output );
274
		$this->assertRegExp( '#<div class="delivery.+<div class="price.+32.00 .+</div>#smU', $output );
275
		$this->assertRegExp( '#<div class="total.+<div class="price.+668.00 .+</div>#smU', $output );
276
	}
277
278
279
	public function testBodyDeleteSingle()
280
	{
281
		$this->addProduct( 'CNE', 2, 'default' );
282
		$this->addProduct( 'CNC', 1, 'default' );
283
284
		$param = array(
285
			'b_action' => 'delete',
286
			'b_position' => 1,
287
		);
288
289
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
290
		$this->view->addHelper( 'param', $helper );
291
292
		$this->object->init();
293
		$output = $this->object->body();
294
295
		$this->assertRegExp( '#<div class="price.+36.00 .+</div>#smU', $output );
296
		$this->assertRegExp( '#<div class="subtotal.+<div class="price.+36.00 .+</div>#smU', $output );
297
		$this->assertRegExp( '#<div class="delivery.+<div class="price.+2.00 .+</div>#smU', $output );
298
		$this->assertRegExp( '#<div class="total.+<div class="price.+38.00 .+</div>#smU', $output );
299
	}
300
301
302
	public function testBodyDeleteMulti()
303
	{
304
		$this->addProduct( 'CNE', 1, 'default' );
305
		$this->addProduct( 'CNC', 1, 'default' );
306
307
		$param = array(
308
			'b_action' => 'delete',
309
			'b_position' => array( 0, 1 ),
310
		);
311
312
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
313
		$this->view->addHelper( 'param', $helper );
314
315
		$this->object->init();
316
		$output = $this->object->body();
317
318
		$this->assertRegExp( '#<div class="total.+<div class="price.+0.00 .+</div>#smU', $output );
319
	}
320
321
322
	public function testBodyDeleteInvalid()
323
	{
324
		$param = array(
325
			'b_action' => 'delete',
326
			'b_position' => -1,
327
		);
328
329
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
330
		$this->view->addHelper( 'param', $helper );
331
332
		$this->object->init();
333
334
		$this->assertEquals( 1, count( $this->view->get( 'errors', [] ) ) );
335
	}
336
337
338
	public function testBodyAddCoupon()
339
	{
340
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
341
		$controller->addProduct( $this->getProductItem( 'CNC' ), 1 );
342
343
344
		$param = array( 'b_coupon' => '90AB' );
345
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
346
		$this->view->addHelper( 'param', $helper );
347
348
		$this->object->init();
349
350
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
351
		$this->view->standardBasket = $controller->get();
352
		$output = $this->object->body();
353
354
		$this->assertRegExp( '#<li class="attr-item">.*90AB.*</li>#smU', $output );
355
	}
356
357
358
	public function testBodyDeleteCoupon()
359
	{
360
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
361
		$controller->addProduct( $this->getProductItem( 'CNC' ), 1 );
362
363
364
		$param = array( 'b_coupon' => '90AB' );
365
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
366
		$this->view->addHelper( 'param', $helper );
367
368
		$this->object->init();
369
370
371
		$param = array( 'b_action' => 'coupon-delete', 'b_coupon' => '90AB' );
372
373
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
374
		$this->view->addHelper( 'param', $helper );
375
376
		$this->object->init();
377
378
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
379
		$this->view->standardBasket = $controller->get();
380
		$output = $this->object->body();
381
382
		$this->assertNotRegExp( '#<ul class="attr-list">#smU', $output );
383
	}
384
385
386
	public function testBodyOverwriteCoupon()
387
	{
388
		$this->context->config()->set( 'client/html/basket/standard/coupon/overwrite', true );
389
390
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
391
		$controller->addProduct( $this->getProductItem( 'CNC' ), 1 );
392
393
394
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['b_coupon' => '90AB'] );
395
		$this->view->addHelper( 'param', $helper );
396
397
		$this->object->init();
398
399
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['b_coupon' => 'OPQR'] );
400
		$this->view->addHelper( 'param', $helper );
401
402
		$this->object->init();
403
404
		$controller = \Aimeos\Controller\Frontend\Basket\Factory::create( $this->context );
405
		$this->view->standardBasket = $controller->get();
406
		$output = $this->object->body();
407
408
		$this->assertStringContainsString( 'OPQR', $output );
409
		$this->assertStringNotContainsString( '90AB', $output );
410
	}
411
412
413
	public function testGetSubClientInvalid()
414
	{
415
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
416
		$this->object->getSubClient( 'invalid', 'invalid' );
417
	}
418
419
420
	public function testGetSubClientInvalidName()
421
	{
422
		$this->expectException( '\\Aimeos\\Client\\Html\\Exception' );
423
		$this->object->getSubClient( '$$$', '$$$' );
424
	}
425
426
427
	/**
428
	 * @param string $code
429
	 * @param integer $quantity
430
	 * @param string $stockType
431
	 */
432
	protected function addProduct( $code, $quantity, $stockType )
433
	{
434
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
435
		$search = $manager->filter();
436
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
437
438
		if( ( $item = $manager->search( $search )->first() ) === null ) {
439
			throw new \RuntimeException( sprintf( 'No product item with code "%1$s" found', $code ) );
440
		}
441
442
		$param = array(
443
			'b_action' => 'add',
444
			'b_prodid' => $item->getId(),
445
			'b_quantity' => $quantity,
446
			'b_stocktype' => $stockType,
447
		);
448
449
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
450
		$this->view->addHelper( 'param', $helper );
451
452
		$this->object->init();
453
	}
454
455
456
	/**
457
	 * @param string $code
458
	 */
459
	protected function getProductItem( $code )
460
	{
461
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
462
		$search = $manager->filter();
463
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
464
465
		if( ( $item = $manager->search( $search, ['price'] )->first() ) === null ) {
466
			throw new \RuntimeException( sprintf( 'No product item with code "%1$s" found', $code ) );
467
		}
468
469
		return $item;
470
	}
471
}
472