StandardTest::testBodyDefaultCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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