Passed
Push — master ( 75274e...ab5b20 )
by Aimeos
03:49
created

StandardTest::testModify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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\Catalog\Detail;
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->view = \TestHelperHtml::view();
23
		$this->context = \TestHelperHtml::context();
24
25
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $this->context );
26
		$this->object->setView( $this->view );
27
	}
28
29
30
	protected function tearDown() : void
31
	{
32
		unset( $this->object, $this->context, $this->view );
33
	}
34
35
36
	public function testHeader()
37
	{
38
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['d_prodid' => $this->getProductItem()->getId()] );
39
		$this->view->addHelper( 'param', $helper );
40
41
		$tags = [];
42
		$expire = null;
43
44
		$this->object->setView( $this->object->data( $this->view, $tags, $expire ) );
45
		$output = $this->object->header();
46
47
		$this->assertStringContainsString( '<title>Cafe Noire Expresso | Aimeos</title>', $output );
48
		$this->assertStringContainsString( '<script defer src="http://baseurl/catalog/stock/?st_pid', $output );
49
		$this->assertEquals( '2098-01-01 00:00:00', $expire );
50
		$this->assertEquals( 6, count( $tags ) );
51
	}
52
53
54
	public function testBody()
55
	{
56
		$params = ['d_prodid' => $this->getProductItem()->getId(), 'd_pos' => 1];
57
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
58
		$this->view->addHelper( 'param', $helper );
59
60
		$this->view->navigationPrev = '#';
61
		$this->view->navigationNext = '#';
62
63
		$tags = [];
64
		$expire = null;
65
66
		$this->object->setView( $this->object->data( $this->view, $tags, $expire ) );
67
		$output = $this->object->body();
68
69
		$this->assertStringContainsString( '<!-- catalog.detail.navigator -->', $output );
70
		$this->assertStringContainsString( '<a class="prev"', $output );
71
		$this->assertStringContainsString( '<a class="next"', $output );
72
73
		$this->assertStringContainsString( '<section class="aimeos catalog-detail"', $output );
74
		$this->assertStringContainsString( '<div class="catalog-detail-basic', $output );
75
		$this->assertStringContainsString( '<div class="catalog-detail-image', $output );
76
		$this->assertStringContainsString( '<div class="catalog-detail-service', $output );
77
78
		$this->assertStringContainsString( '<div class="catalog-social">', $output );
79
		$this->assertStringContainsString( 'facebook', $output );
80
81
		$this->assertStringContainsString( '<div class="catalog-actions', $output );
82
		$this->assertStringContainsString( 'actions-button-pin', $output );
83
		$this->assertStringContainsString( 'actions-button-watch', $output );
84
		$this->assertStringContainsString( 'actions-button-favorite', $output );
85
86
		$this->assertStringContainsString( 'catalog-detail-additional', $output );
87
88
		$this->assertStringContainsString( '<td class="name">size</td>', $output );
89
		$this->assertStringContainsString( '<span class="attr-name">XS</span>', $output );
90
		$this->assertStringContainsString( '<td class="name">package-height</td>', $output );
91
		$this->assertStringContainsString( '<td class="value">10.0</td>', $output );
92
93
		$this->assertStringContainsString( '<span class="media-name">Example image</span>', $output );
94
95
		$this->assertStringContainsString( '<section class="catalog-detail-suggest', $output );
96
		$this->assertStringContainsString( 'Cappuccino', $output );
97
98
		$this->assertStringContainsString( '<section class="catalog-detail-bought', $output );
99
		$this->assertStringContainsString( 'Cappuccino', $output );
100
101
		$this->assertStringContainsString( '<div class="catalog-detail-service', $output );
102
		$this->assertStringContainsString( '<div class="catalog-detail-supplier', $output );
103
104
		$this->assertEquals( '2098-01-01 00:00:00', $expire );
105
		$this->assertEquals( 6, count( $tags ) );
106
107
		$result = $this->context->session()->get( 'aimeos/catalog/session/seen/list' );
108
		$this->assertIsArray( $result );
109
		$this->assertEquals( 1, count( $result ) );
110
	}
111
112
113
	public function testBodyByName()
114
	{
115
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $this->context );
116
		$this->object->setView( \TestHelperHtml::view() );
117
118
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, array( 'd_name' => 'Cafe-Noire-Expresso' ) );
119
		$this->view->addHelper( 'param', $helper );
120
121
		$this->object->setView( $this->object->data( $this->view ) );
122
		$output = $this->object->body();
123
124
		$this->assertStringContainsString( '<span class="value" itemprop="sku">CNE</span>', $output );
125
	}
126
127
128
	public function testBodyDefaultId()
129
	{
130
		$context = clone $this->context;
131
		$context->config()->set( 'client/html/catalog/detail/prodid-default', $this->getProductItem()->getId() );
132
133
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $context );
134
		$this->object->setView( \TestHelperHtml::view() );
135
136
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, [] );
137
		$this->view->addHelper( 'param', $helper );
138
139
		$this->object->setView( $this->object->data( $this->view ) );
140
		$output = $this->object->body();
141
142
		$this->assertStringContainsString( '<span class="value" itemprop="sku">CNE</span>', $output );
143
	}
144
145
146
	public function testBodyDefaultCode()
147
	{
148
		$context = clone $this->context;
149
		$context->config()->set( 'client/html/catalog/detail/prodcode-default', 'CNE' );
150
151
		$this->object = new \Aimeos\Client\Html\Catalog\Detail\Standard( $context );
152
		$this->object->setView( \TestHelperHtml::view() );
153
154
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, [] );
155
		$this->view->addHelper( 'param', $helper );
156
157
		$this->object->setView( $this->object->data( $this->view ) );
158
		$output = $this->object->body();
159
160
		$this->assertStringContainsString( '<span class="value" itemprop="sku">CNE</span>', $output );
161
	}
162
163
164
	public function testBodyCsrf()
165
	{
166
		$item = $this->getProductItem();
167
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['d_prodid' => $item->getId()] );
168
		$this->view->addHelper( 'param', $helper );
169
		$this->view->detailProductItem = $item;
170
171
		$output = $this->object->body( 1 );
172
		$output = str_replace( '_csrf_value', '_csrf_new', $output );
173
174
		$this->assertStringContainsString( '<input class="csrf-token" type="hidden" name="_csrf_token" value="_csrf_new"', $output );
175
176
		$output = $this->object->modify( $output, 1 );
177
178
		$this->assertStringContainsString( '<input class="csrf-token" type="hidden" name="_csrf_token" value="_csrf_value"', $output );
179
	}
180
181
182
	public function testBodyAttributes()
183
	{
184
		$product = $this->getProductItem( 'U:TESTP', array( 'attribute' ) );
185
186
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, array( 'd_prodid' => $product->getId() ) );
187
		$this->view->addHelper( 'param', $helper );
188
189
		$configAttr = $product->getRefItems( 'attribute', null, 'config' );
190
191
		$this->assertGreaterThan( 0, count( $configAttr ) );
192
193
		$output = $this->object->body();
194
		$this->assertStringContainsString( '<div class="catalog-detail-basket-attribute', $output );
195
196
		foreach( $configAttr as $id => $item ) {
197
			$this->assertRegexp( '#<option class="select-option".*value="' . $id . '">#smU', $output );
198
		}
199
	}
200
201
202
	public function testBodySelection()
203
	{
204
		$prodId = $this->getProductItem( 'U:TEST' )->getId();
205
206
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, array( 'd_prodid' => $prodId ) );
207
		$this->view->addHelper( 'param', $helper );
208
209
		$variantAttr1 = $this->getProductItem( 'U:TESTSUB02', array( 'attribute' ) )->getRefItems( 'attribute', null, 'variant' );
210
		$variantAttr2 = $this->getProductItem( 'U:TESTSUB04', array( 'attribute' ) )->getRefItems( 'attribute', null, 'variant' );
211
212
		$this->assertGreaterThan( 0, count( $variantAttr1 ) );
213
		$this->assertGreaterThan( 0, count( $variantAttr2 ) );
214
215
		$tags = [];
216
		$expire = null;
217
218
		$this->object->setView( $this->object->data( $this->view, $tags, $expire ) );
219
		$output = $this->object->body( 1, $tags, $expire );
220
221
		$this->assertStringContainsString( '<div class="catalog-detail-basket-selection', $output );
222
223
		foreach( $variantAttr1 as $id => $item ) {
224
			$this->assertRegexp( '#<option class="select-option" value="' . $id . '">#', $output );
225
		}
226
227
		foreach( $variantAttr2 as $id => $item ) {
228
			$this->assertRegexp( '#<option class="select-option" value="' . $id . '">#', $output );
229
		}
230
231
		$this->assertEquals( null, $expire );
232
		$this->assertEquals( 5, count( $tags ) );
233
	}
234
235
236
	public function testModify()
237
	{
238
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, array( 'd_pos' => 1 ) );
239
		$this->view->addHelper( 'param', $helper );
240
241
		$content = '<!-- catalog.detail.navigator -->test<!-- catalog.detail.navigator -->';
242
		$output = $this->object->modify( $content, 1 );
243
244
		$this->assertStringContainsString( '<div class="catalog-detail-navigator">', $output );
245
	}
246
247
248
	public function testInit()
249
	{
250
		$prodid = $this->getProductItem()->getId();
251
252
		$session = $this->context->session();
253
		$session->set( 'aimeos/catalog/session/seen/list', array( $prodid => 'test' ) );
254
		$session->set( 'aimeos/catalog/session/seen/cache', array( $prodid => 'test' ) );
255
256
		$param = array( 'd_prodid' => $prodid );
257
258
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
259
		$this->view->addHelper( 'param', $helper );
260
261
		$this->object->init();
262
263
		$str = $session->get( 'aimeos/catalog/session/seen/list' );
264
		$this->assertIsArray( $str );
265
	}
266
267
268
	protected function getProductItem( $code = 'CNE', $domains = [] )
269
	{
270
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
271
		$search = $manager->filter();
272
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
273
274
		if( ( $item = $manager->search( $search, $domains )->first() ) === null ) {
275
			throw new \RuntimeException( sprintf( 'No product item with code "%1$s" found', $code ) );
276
		}
277
278
		return $item;
279
	}
280
}
281