Completed
Push — 2016.04 ( 690368...e61816 )
by Aimeos
04:37
created

Typo3Test::testMoveItemFirstToLast()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 27
rs 8.5806
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, 2013
6
 * @copyright Aimeos (aimeos.org), 2014
7
 */
8
9
10
namespace Aimeos\MShop\Customer\Manager\Lists;
11
12
13
/**
14
 * Test class for \Aimeos\MShop\Customer\Manager\Lists\Typo3Test.
15
 */
16
class Typo3Test extends \PHPUnit_Framework_TestCase
17
{
18
	private $object;
19
	private $context;
20
	private $editor = '';
21
22
23
	/**
24
	 * Sets up the fixture.
25
	 * This method is called before a test is executed.
26
	 *
27
	 * @access protected
28
	 */
29
	protected function setUp()
30
	{
31
		$this->context = \TestHelper::getContext();
32
		$this->editor = $this->context->getEditor();
33
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( \TestHelper::getContext(), 'Typo3' );
34
		$this->object = $manager->getSubManager( 'lists', 'Typo3' );
35
	}
36
37
38
	/**
39
	 * Tears down the fixture, for example, closes a network connection.
40
	 * This method is called after a test is executed.
41
	 *
42
	 * @access protected
43
	 */
44
	protected function tearDown()
45
	{
46
		unset( $this->object );
47
		\Aimeos\MShop\Factory::clear();
48
	}
49
50
51
	public function testAggregate()
52
	{
53
		$search = $this->object->createSearch( true );
54
		$result = $this->object->aggregate( $search, 'customer.lists.domain' );
55
56
		$this->assertEquals( 1, count( $result ) );
57
		$this->assertArrayHasKey( 'text', $result );
58
		$this->assertEquals( 4, $result['text'] );
59
	}
60
61
62
	public function testCreateItem()
63
	{
64
		$item = $this->object->createItem();
65
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $item );
66
	}
67
68
69
	public function testGetItem()
70
	{
71
		$search = $this->object->createSearch();
72
		$search->setSlice(0, 1);
73
		$results = $this->object->searchItems( $search );
74
75
		if( ( $item = reset( $results ) ) === false ) {
76
			throw new \Exception( 'No item found' );
77
		}
78
79
		$this->assertEquals( $item, $this->object->getItem( $item->getId() ) );
80
	}
81
82
83
	public function testSaveUpdateDeleteItem()
84
	{
85
		$search = $this->object->createSearch();
86
		$search->setSlice(0, 1);
87
		$items = $this->object->searchItems( $search );
88
89
		if( ( $item = reset( $items ) ) === false ) {
90
			throw new \Exception( 'No item found' );
91
		}
92
93
		$item->setId(null);
94
		$item->setDomain( 'unittest' );
95
		$this->object->saveItem( $item );
96
		$itemSaved = $this->object->getItem( $item->getId() );
97
98
		$itemExp = clone $itemSaved;
99
		$itemExp->setDomain( 'unittest2' );
100
		$this->object->saveItem( $itemExp );
101
		$itemUpd = $this->object->getItem( $itemExp->getId() );
102
103
		$this->object->deleteItem( $itemSaved->getId() );
104
105
106
		$this->assertTrue( $item->getId() !== null );
107
		$this->assertTrue( $itemSaved->getType() !== null );
108
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
109
		$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
110
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
111
		$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
112
		$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
113
		$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
114
		$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
115
		$this->assertEquals( $item->getDateEnd(), $itemSaved->getDateEnd() );
116
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
117
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
118
		$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeCreated());
119
		$this->assertStringStartsWith(date('Y-m-d', time()), $itemSaved->getTimeModified());
120
121
		$this->assertEquals( $this->editor, $itemSaved->getEditor() );
122
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
123
		$this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );
124
125
		$this->assertTrue( $itemUpd->getType() !== null );
126
		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
127
		$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
128
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
129
		$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
130
		$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() );
131
		$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
132
		$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
133
		$this->assertEquals( $itemExp->getDateEnd(), $itemUpd->getDateEnd() );
134
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
135
136
		$this->assertEquals( $this->editor, $itemUpd->getEditor() );
137
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
138
		$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );
139
140
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
141
		$this->object->getItem( $itemSaved->getId() );
142
	}
143
144
145
	public function testMoveItemLastToFront()
146
	{
147
		$listItems = $this->getListItems();
148
		$this->assertGreaterThan( 1, count( $listItems ) );
149
150
		if( ( $first = reset( $listItems ) ) === false ) {
151
			throw new \Exception( 'No first customer list item' );
152
		}
153
154
		if( ( $last = end( $listItems ) ) === false ) {
155
			throw new \Exception( 'No last customer list item' );
156
		}
157
158
		$this->object->moveItem( $last->getId(), $first->getId() );
159
160
		$newFirst = $this->object->getItem( $last->getId() );
161
		$newSecond = $this->object->getItem( $first->getId() );
162
163
		$this->object->moveItem( $last->getId() );
164
165
		$this->assertEquals( 0, $newFirst->getPosition() );
166
		$this->assertEquals( 1, $newSecond->getPosition() );
167
	}
168
169
170
	public function testMoveItemFirstToLast()
171
	{
172
		$listItems = $this->getListItems();
173
		$this->assertGreaterThan( 1, count( $listItems ) );
174
175
		if( ( $first = reset( $listItems ) ) === false ) {
176
			throw new \Exception( 'No first customer list item' );
177
		}
178
179
		if( ( $second = next( $listItems ) ) === false ) {
180
			throw new \Exception( 'No second customer list item' );
181
		}
182
183
		if( ( $last = end( $listItems ) ) === false ) {
184
			throw new \Exception( 'No last customer list item' );
185
		}
186
187
		$this->object->moveItem( $first->getId() );
188
189
		$newBefore = $this->object->getItem( $last->getId() );
190
		$newLast = $this->object->getItem( $first->getId() );
191
192
		$this->object->moveItem( $first->getId(), $second->getId() );
193
194
		$this->assertEquals( $last->getPosition() - 1, $newBefore->getPosition() );
195
		$this->assertEquals( $last->getPosition(), $newLast->getPosition() );
196
	}
197
198
199
	public function testMoveItemFirstUp()
200
	{
201
		$listItems = $this->getListItems();
202
		$this->assertGreaterThan( 1, count( $listItems ) );
203
204
		if( ( $first = reset( $listItems ) ) === false ) {
205
			throw new \Exception( 'No first customer list item' );
206
		}
207
208
		if( ( $second = next( $listItems ) ) === false ) {
209
			throw new \Exception( 'No second customer list item' );
210
		}
211
212
		if( ( $last = end( $listItems ) ) === false ) {
213
			throw new \Exception( 'No last customer list item' );
214
		}
215
216
		$this->object->moveItem( $first->getId(), $last->getId() );
217
218
		$newLast = $this->object->getItem( $last->getId() );
219
		$newUp = $this->object->getItem( $first->getId() );
220
221
		$this->object->moveItem( $first->getId(), $second->getId() );
222
223
		$this->assertEquals( $last->getPosition() - 1, $newUp->getPosition() );
224
		$this->assertEquals( $last->getPosition(), $newLast->getPosition() );
225
	}
226
227
228
	public function testSearchItems()
229
	{
230
		$total = 0;
231
		$search = $this->object->createSearch();
232
233
		$expr = array();
234
		$expr[] = $search->compare( '!=', 'customer.lists.id', null );
235
		$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
236
		$expr[] = $search->compare( '>', 'customer.lists.parentid', 0 );
237
		$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
238
		$expr[] = $search->compare( '>', 'customer.lists.typeid', 0 );
239
		$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
240
		$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
241
		$expr[] = $search->compare( '==', 'customer.lists.dateend', '2022-01-01 00:00:00' );
242
		$expr[] = $search->compare( '>', 'customer.lists.position', 0 );
243
		$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
244
		$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
245
		$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
246
		$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );
247
248
		$expr[] = $search->compare( '!=', 'customer.lists.type.id', 0 );
249
		$expr[] = $search->compare( '!=', 'customer.lists.type.siteid', null );
250
		$expr[] = $search->compare( '==', 'customer.lists.type.code', 'default' );
251
		$expr[] = $search->compare( '==', 'customer.lists.type.domain', 'text' );
252
		$expr[] = $search->compare( '==', 'customer.lists.type.label', 'Standard' );
253
		$expr[] = $search->compare( '==', 'customer.lists.type.status', 1 );
254
		$expr[] = $search->compare( '>=', 'customer.lists.type.mtime', '1970-01-01 00:00:00' );
255
		$expr[] = $search->compare( '>=', 'customer.lists.type.ctime', '1970-01-01 00:00:00' );
256
		$expr[] = $search->compare( '==', 'customer.lists.type.editor', $this->editor );
257
258
		$search->setConditions( $search->combine( '&&', $expr ) );
259
		$search->setSlice(0, 2);
260
		$results = $this->object->searchItems( $search, array(), $total );
261
		$this->assertEquals( 2, count( $results ) );
262
		$this->assertEquals( 3, $total );
263
264
		foreach($results as $itemId => $item) {
265
			$this->assertEquals( $itemId, $item->getId() );
266
		}
267
268
		//search without base criteria
269
		$search = $this->object->createSearch();
270
		$search->setConditions( $search->compare( '==', 'customer.lists.editor', $this->editor ) );
271
		$this->assertEquals( 4, count( $this->object->searchItems($search) ) );
272
273
		//search with base criteria
274
		$search = $this->object->createSearch(true);
275
		$conditions = array(
276
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
277
			$search->getConditions()
278
		);
279
		$search->setConditions( $search->combine( '&&', $conditions ) );
280
		$this->assertEquals( 4, count( $this->object->searchItems($search) ) );
281
	}
282
283
284
	public function testGetSubManager()
285
	{
286
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type') );
287
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Manager\\Iface', $this->object->getSubManager('type', 'Typo3') );
288
289
		$this->setExpectedException('\\Aimeos\\MShop\\Exception');
290
		$this->object->getSubManager('unknown');
291
	}
292
293
294
	protected function getListItems()
295
	{
296
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->context, 'Typo3' );
297
298
		$search = $manager->createSearch();
299
		$search->setConditions( $search->compare( '==', 'customer.code', '[email protected]' ) );
300
		$search->setSlice( 0, 1 );
301
302
		$results = $manager->searchItems( $search );
303
304
		if( ( $item = reset( $results ) ) === false ) {
305
			throw new \Exception( 'No customer item found' );
306
		}
307
308
		$search = $this->object->createSearch();
309
		$expr = array(
310
			$search->compare( '==', 'customer.lists.parentid', $item->getId() ),
311
			$search->compare( '==', 'customer.lists.domain', 'text' ),
312
			$search->compare( '==', 'customer.lists.editor', $this->editor ),
313
			$search->compare( '==', 'customer.lists.type.code', 'default' ),
314
		);
315
		$search->setConditions( $search->combine( '&&', $expr ) );
316
		$search->setSortations( array( $search->sort( '+', 'customer.lists.position' ) ) );
317
318
		return $this->object->searchItems( $search );
319
	}
320
}
321