Passed
Push — master ( dc5862...f6527a )
by Aimeos
04:15 queued 13s
created

StandardTest::testBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

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