Completed
Push — tree_move_visibility ( 054a54 )
by André
28:14 queued 03:07
created

DoctrineDatabaseTest::testMoveHiddenUpdate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 28

Duplication

Lines 38
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 38
loc 38
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\Location\Gateway\DoctrineDatabaseTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\Location\Gateway;
12
13
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
14
use eZ\Publish\SPI\Persistence\Content\Location;
15
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct;
16
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase;
17
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
18
19
/**
20
 * Test case for eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase.
21
 */
22
class DoctrineDatabaseTest extends TestCase
23
{
24 View Code Duplication
    protected function getLocationGateway()
25
    {
26
        $dbHandler = $this->getDatabaseHandler();
27
28
        return new DoctrineDatabase(
29
            $dbHandler,
30
            $this
31
                ->getMockBuilder('eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\Location\\Gateway\\CriteriaConverter')
32
                ->disableOriginalConstructor()
33
                ->getMock(),
34
            $this
35
                ->getMockBuilder('eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\Location\\Gateway\\SortClauseConverter')
36
                ->disableOriginalConstructor()
37
                ->getMock()
38
        );
39
    }
40
41 View Code Duplication
    public static function getLoadLocationValues()
42
    {
43
        return array(
44
            array('node_id', 77),
45
            array('priority', 0),
46
            array('is_hidden', 0),
47
            array('is_invisible', 0),
48
            array('remote_id', 'dbc2f3c8716c12f32c379dbf0b1cb133'),
49
            array('contentobject_id', 75),
50
            array('parent_node_id', 2),
51
            array('path_identification_string', 'solutions'),
52
            array('path_string', '/1/2/77/'),
53
            array('modified_subnode', 1311065017),
54
            array('main_node_id', 77),
55
            array('depth', 2),
56
            array('sort_field', 2),
57
            array('sort_order', 1),
58
        );
59
    }
60
61
    /**
62
     * @dataProvider getLoadLocationValues
63
     */
64 View Code Duplication
    public function testLoadLocationByRemoteId($field, $value)
65
    {
66
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
67
        $handler = $this->getLocationGateway();
68
        $data = $handler->getBasicNodeDataByRemoteId('dbc2f3c8716c12f32c379dbf0b1cb133');
69
70
        $this->assertEquals(
71
            $value,
72
            $data[$field],
73
            "Value in property $field not as expected."
74
        );
75
    }
76
77
    /**
78
     * @dataProvider getLoadLocationValues
79
     */
80
    public function testLoadLocation($field, $value)
81
    {
82
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
83
        $handler = $this->getLocationGateway();
84
        $data = $handler->getBasicNodeData(77);
85
86
        $this->assertEquals(
87
            $value,
88
            $data[$field],
89
            "Value in property $field not as expected."
90
        );
91
    }
92
93
    /**
94
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
95
     */
96
    public function testLoadInvalidLocation()
97
    {
98
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
99
        $handler = $this->getLocationGateway();
100
        $data = $handler->getBasicNodeData(1337);
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
    }
102
103
    /**
104
     * @dataProvider getLoadLocationValues
105
     */
106 View Code Duplication
    public function testLoadLocationDataByContent($field, $value)
107
    {
108
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
109
110
        $gateway = $this->getLocationGateway();
111
112
        $locationsData = $gateway->loadLocationDataByContent(75);
113
114
        $this->assertCount(1, $locationsData);
115
116
        $locationRow = reset($locationsData);
117
118
        $this->assertEquals($value, $locationRow[$field]);
119
    }
120
121
    /**
122
     * @dataProvider getLoadLocationValues
123
     */
124 View Code Duplication
    public function testLoadParentLocationDataForDraftContentAll($field, $value)
125
    {
126
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
127
128
        $gateway = $this->getLocationGateway();
129
130
        $locationsData = $gateway->loadParentLocationsDataForDraftContent(226);
131
132
        $this->assertCount(1, $locationsData);
133
134
        $locationRow = reset($locationsData);
135
136
        $this->assertEquals($value, $locationRow[$field]);
137
    }
138
139
    public function testLoadLocationDataByContentLimitSubtree()
140
    {
141
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
142
143
        $gateway = $this->getLocationGateway();
144
145
        $locationsData = $gateway->loadLocationDataByContent(75, 3);
146
147
        $this->assertCount(0, $locationsData);
148
    }
149
150 View Code Duplication
    public function testMoveSubtreePathUpdate()
151
    {
152
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
153
        $handler = $this->getLocationGateway();
154
        $handler->moveSubtreeNodes(
155
            array(
156
                'path_string' => '/1/2/69/',
157
                'path_identification_string' => 'products',
158
                'is_hidden' => 0,
159
                'is_invisible' => 0,
160
            ),
161
            array(
162
                'path_string' => '/1/2/77/',
163
                'path_identification_string' => 'solutions',
164
                'is_hidden' => 0,
165
                'is_invisible' => 0,
166
            )
167
        );
168
169
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
170
        $query = $this->handler->createSelectQuery();
171
        $this->assertQueryResult(
172
            array(
173
                array(65, '/1/2/', '', 1, 1, 0, 0),
174
                array(67, '/1/2/77/69/', 'solutions/products', 77, 3, 0, 0),
175
                array(69, '/1/2/77/69/70/71/', 'solutions/products/software/os_type_i', 70, 5, 0, 0),
176
                array(73, '/1/2/77/69/72/75/', 'solutions/products/boxes/cd_dvd_box_iii', 72, 5, 0, 0),
177
                array(75, '/1/2/77/', 'solutions', 2, 2, 0, 0),
178
            ),
179
            $query
180
                ->select('contentobject_id', 'path_string', 'path_identification_string', 'parent_node_id', 'depth', 'is_hidden', 'is_invisible')
181
                ->from('ezcontentobject_tree')
182
                ->where($query->expr->in('node_id', array(69, 71, 75, 77, 2)))
183
                ->orderBy('contentobject_id')
184
        );
185
    }
186
187 View Code Duplication
    public function testMoveHiddenUpdate()
188
    {
189
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
190
        $handler = $this->getLocationGateway();
191
        $handler->hideSubtree('/1/2/77/');
192
        $handler->moveSubtreeNodes(
193
            array(
194
                'path_string' => '/1/2/69/',
195
                'path_identification_string' => 'products',
196
                'is_hidden' => 0,
197
                'is_invisible' => 0,
198
            ),
199
            array(
200
                'path_string' => '/1/2/77/',
201
                'path_identification_string' => 'solutions',
202
                'is_hidden' => 1,
203
                'is_invisible' => 1,
204
205
            )
206
        );
207
208
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
209
        $query = $this->handler->createSelectQuery();
210
        $this->assertQueryResult(
211
            array(
212
                array(65, '/1/2/', '', 1, 1, 0, 0),
213
                array(67, '/1/2/77/69/', 'solutions/products', 77, 3, 0, 1),
214
                array(69, '/1/2/77/69/70/71/', 'solutions/products/software/os_type_i', 70, 5, 0, 1),
215
                array(73, '/1/2/77/69/72/75/', 'solutions/products/boxes/cd_dvd_box_iii', 72, 5, 0, 1),
216
                array(75, '/1/2/77/', 'solutions', 2, 2, 1, 1),
217
            ),
218
            $query
219
                ->select('contentobject_id', 'path_string', 'path_identification_string', 'parent_node_id', 'depth', 'is_hidden', 'is_invisible')
220
                ->from('ezcontentobject_tree')
221
                ->where($query->expr->in('node_id', array(69, 71, 75, 77, 2)))
222
                ->orderBy('contentobject_id')
223
        );
224
    }
225
226
    public function testMoveSubtreeAssignmentUpdate()
227
    {
228
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
229
        $handler = $this->getLocationGateway();
230
        $handler->updateNodeAssignment(67, 2, 77, 5);
231
232
        $query = $this->handler->createSelectQuery();
233
        $this->assertQueryResult(
234
            array(
235
                array(67, 1, 0, 53, 1, 5, 77, '9cec85d730eec7578190ee95ce5a36f5', 0, 2, 1, 0, 0),
236
            ),
237
            $query
238
                ->select('*')
239
                ->from('eznode_assignment')
240
                ->where($query->expr->eq('contentobject_id', 67))
241
        );
242
    }
243
244
    public function testUpdateSubtreeModificationTime()
245
    {
246
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
247
        $handler = $this->getLocationGateway();
248
        $time = time();
249
        $handler->updateSubtreeModificationTime('/1/2/69/');
250
251
        $query = $this->handler->createSelectQuery();
252
        $this->assertQueryResult(
253
            array(
254
                array('/1/'),
255
                array('/1/2/'),
256
                array('/1/2/69/'),
257
            ),
258
            $query
259
                ->select('path_string')
260
                ->from('ezcontentobject_tree')
261
                ->where($query->expr->gte('modified_subnode', $time))
262
                ->orderBy('path_string')
263
        );
264
    }
265
266 View Code Duplication
    public function testHideUpdateHidden()
267
    {
268
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
269
        $handler = $this->getLocationGateway();
270
        $handler->hideSubtree('/1/2/69/');
271
272
        $query = $this->handler->createSelectQuery();
273
        $this->assertQueryResult(
274
            array(
275
                array(1, 0, 0),
276
                array(2, 0, 0),
277
                array(69, 1, 1),
278
                array(75, 0, 1),
279
            ),
280
            $query
281
                ->select('node_id', 'is_hidden', 'is_invisible')
282
                ->from('ezcontentobject_tree')
283
                ->where($query->expr->in('node_id', array(1, 2, 69, 75)))
284
                ->orderBy('node_id')
285
        );
286
    }
287
288
    /**
289
     * @depends testHideUpdateHidden
290
     */
291 View Code Duplication
    public function testHideUnhideUpdateHidden()
292
    {
293
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
294
        $handler = $this->getLocationGateway();
295
        $handler->hideSubtree('/1/2/69/');
296
        $handler->unhideSubtree('/1/2/69/');
297
298
        $query = $this->handler->createSelectQuery();
299
        $this->assertQueryResult(
300
            array(
301
                array(1, 0, 0),
302
                array(2, 0, 0),
303
                array(69, 0, 0),
304
                array(75, 0, 0),
305
            ),
306
            $query
307
                ->select('node_id', 'is_hidden', 'is_invisible')
308
                ->from('ezcontentobject_tree')
309
                ->where($query->expr->in('node_id', array(1, 2, 69, 75)))
310
                ->orderBy('node_id')
311
        );
312
    }
313
314
    /**
315
     * @depends testHideUpdateHidden
316
     */
317 View Code Duplication
    public function testHideUnhideParentTree()
318
    {
319
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
320
        $handler = $this->getLocationGateway();
321
        $handler->hideSubtree('/1/2/69/');
322
        $handler->hideSubtree('/1/2/69/70/');
323
        $handler->unhideSubtree('/1/2/69/');
324
325
        $query = $this->handler->createSelectQuery();
326
        $this->assertQueryResult(
327
            array(
328
                array(1, 0, 0),
329
                array(2, 0, 0),
330
                array(69, 0, 0),
331
                array(70, 1, 1),
332
                array(71, 0, 1),
333
                array(75, 0, 0),
334
            ),
335
            $query
336
                ->select('node_id', 'is_hidden', 'is_invisible')
337
                ->from('ezcontentobject_tree')
338
                ->where($query->expr->in('node_id', array(1, 2, 69, 70, 71, 75)))
339
                ->orderBy('node_id')
340
        );
341
    }
342
343
    /**
344
     * @depends testHideUpdateHidden
345
     */
346 View Code Duplication
    public function testHideUnhidePartialSubtree()
347
    {
348
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
349
        $handler = $this->getLocationGateway();
350
        $handler->hideSubtree('/1/2/69/');
351
        $handler->hideSubtree('/1/2/69/70/');
352
        $handler->unhideSubtree('/1/2/69/70/');
353
354
        $query = $this->handler->createSelectQuery();
355
        $this->assertQueryResult(
356
            array(
357
                array(1, 0, 0),
358
                array(2, 0, 0),
359
                array(69, 1, 1),
360
                array(70, 0, 1),
361
                array(71, 0, 1),
362
                array(75, 0, 1),
363
            ),
364
            $query
365
                ->select('node_id', 'is_hidden', 'is_invisible')
366
                ->from('ezcontentobject_tree')
367
                ->where($query->expr->in('node_id', array(1, 2, 69, 70, 71, 75)))
368
                ->orderBy('node_id')
369
        );
370
    }
371
372
    public function testSwapLocations()
373
    {
374
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
375
        $handler = $this->getLocationGateway();
376
        $handler->swap(70, 78);
377
378
        $query = $this->handler->createSelectQuery();
379
        $this->assertQueryResult(
380
            array(
381
                array(70, 76),
382
                array(78, 68),
383
            ),
384
            $query
385
                ->select('node_id', 'contentobject_id')
386
                ->from('ezcontentobject_tree')
387
                ->where($query->expr->in('node_id', array(70, 78)))
388
                ->orderBy('node_id')
389
        );
390
    }
391
392
    public function testCreateLocation()
393
    {
394
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
395
        $handler = $this->getLocationGateway();
396
        $handler->create(
397
            new CreateStruct(
398
                array(
399
                    'contentId' => 68,
400
                    'remoteId' => 'some_id',
401
                )
402
            ),
403
            array(
404
                'node_id' => '77',
405
                'depth' => '2',
406
                'path_string' => '/1/2/77/',
407
            )
408
        );
409
410
        $query = $this->handler->createSelectQuery();
411
        $this->assertQueryResult(
412
            array(
413
                array(70, '/1/2/69/70/'),
414
                array(77, '/1/2/77/'),
415
                array(228, '/1/2/77/228/'),
416
            ),
417
            $query
418
                ->select('node_id', 'path_string')
419
                ->from('ezcontentobject_tree')
420
                ->where($query->expr->in('contentobject_id', array(68, 75)))
421
                ->orderBy('node_id')
422
        );
423
    }
424
425
    /**
426
     * @covers eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::getMainNodeId
427
     * @depends testCreateLocation
428
     */
429
    public function testGetMainNodeId()
430
    {
431
        // $this->insertDatabaseFixture( __DIR__ . '/_fixtures/full_example_tree.php' );
432
        $handler = $this->getLocationGateway();
433
434
        $parentLocationData = array(
435
            'node_id' => '77',
436
            'depth' => '2',
437
            'path_string' => '/1/2/77/',
438
        );
439
440
        // main location
441
        $mainLocation = $handler->create(
442
            new CreateStruct(
443
                array(
444
                    'contentId' => 68,
445
                    'contentVersion' => 1,
446
                    'remoteId' => 'some_id',
447
                    'mainLocationId' => true,
448
                )
449
            ),
450
            $parentLocationData
451
        );
452
453
        // secondary location
454
        $handler->create(
455
            new CreateStruct(
456
                array(
457
                    'contentId' => 68,
458
                    'contentVersion' => 1,
459
                    'remoteId' => 'some_id',
460
                    'mainLocationId' => $mainLocation->id,
461
                )
462
            ),
463
            $parentLocationData
464
        );
465
466
        $handlerReflection = new \ReflectionObject($handler);
467
        $methodReflection = $handlerReflection->getMethod('getMainNodeId');
468
        $methodReflection->setAccessible(true);
469
        self::assertEquals($mainLocation->id, $res = $methodReflection->invoke($handler, 68));
470
    }
471
472 View Code Duplication
    public static function getCreateLocationValues()
473
    {
474
        return array(
475
            array('contentobject_id', 68),
476
            array('contentobject_is_published', 1),
477
            array('contentobject_version', 1),
478
            array('depth', 3),
479
            array('is_hidden', 0),
480
            array('is_invisible', 0),
481
            array('main_node_id', 42),
482
            array('parent_node_id', 77),
483
            array('path_identification_string', ''),
484
            array('priority', 1),
485
            array('remote_id', 'some_id'),
486
            array('sort_field', 1),
487
            array('sort_order', 1),
488
        );
489
    }
490
491
    /**
492
     * @depends testCreateLocation
493
     * @dataProvider getCreateLocationValues
494
     */
495
    public function testCreateLocationValues($field, $value)
496
    {
497
        if ($value === null) {
498
            $this->markTestIncomplete('Proper value setting yet unknown.');
499
        }
500
501
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
502
        $handler = $this->getLocationGateway();
503
        $handler->create(
504
            new CreateStruct(
505
                array(
506
                    'contentId' => 68,
507
                    'contentVersion' => 1,
508
                    'mainLocationId' => 42,
509
                    'priority' => 1,
510
                    'remoteId' => 'some_id',
511
                    'sortField' => 1,
512
                    'sortOrder' => 1,
513
                )
514
            ),
515
            array(
516
                'node_id' => '77',
517
                'depth' => '2',
518
                'path_string' => '/1/2/77/',
519
            )
520
        );
521
522
        $query = $this->handler->createSelectQuery();
523
        $this->assertQueryResult(
524
            array(array($value)),
525
            $query
526
                ->select($field)
527
                ->from('ezcontentobject_tree')
528
                ->where($query->expr->eq('node_id', 228))
529
        );
530
    }
531
532
    public static function getCreateLocationReturnValues()
533
    {
534
        return array(
535
            array('id', 228),
536
            array('priority', 1),
537
            array('hidden', false),
538
            array('invisible', false),
539
            array('remoteId', 'some_id'),
540
            array('contentId', '68'),
541
            array('parentId', '77'),
542
            array('pathIdentificationString', ''),
543
            array('pathString', '/1/2/77/228/'),
544
            array('depth', 3),
545
            array('sortField', 1),
546
            array('sortOrder', 1),
547
        );
548
    }
549
550
    /**
551
     * @depends testCreateLocation
552
     * @dataProvider getCreateLocationReturnValues
553
     */
554
    public function testCreateLocationReturnValues($field, $value)
555
    {
556
        if ($value === null) {
557
            $this->markTestIncomplete('Proper value setting yet unknown.');
558
        }
559
560
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
561
        $handler = $this->getLocationGateway();
562
        $location = $handler->create(
563
            new CreateStruct(
564
                array(
565
                    'contentId' => 68,
566
                    'contentVersion' => 1,
567
                    'mainLocationId' => true,
568
                    'priority' => 1,
569
                    'remoteId' => 'some_id',
570
                    'sortField' => 1,
571
                    'sortOrder' => 1,
572
                )
573
            ),
574
            array(
575
                'node_id' => '77',
576
                'depth' => '2',
577
                'path_string' => '/1/2/77/',
578
            )
579
        );
580
581
        $this->assertTrue($location instanceof Location);
582
        $this->assertEquals($value, $location->$field);
583
    }
584
585
    public static function getUpdateLocationData()
586
    {
587
        return array(
588
            array('priority', 23),
589
            array('remote_id', 'someNewHash'),
590
            array('sort_field', 4),
591
            array('sort_order', 4),
592
        );
593
    }
594
595
    /**
596
     * @dataProvider getUpdateLocationData
597
     */
598 View Code Duplication
    public function testUpdateLocation($field, $value)
599
    {
600
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
601
        $handler = $this->getLocationGateway();
602
        $handler->update(
603
            new Location\UpdateStruct(
604
                array(
605
                    'priority' => 23,
606
                    'remoteId' => 'someNewHash',
607
                    'sortField' => 4,
608
                    'sortOrder' => 4,
609
                )
610
            ),
611
            70
612
        );
613
614
        $query = $this->handler->createSelectQuery();
615
        $this->assertQueryResult(
616
            array(array($value)),
617
            $query
618
                ->select($field)
619
                ->from('ezcontentobject_tree')
620
                ->where($query->expr->in('node_id', array(70)))
621
        );
622
    }
623
624 View Code Duplication
    public static function getNodeAssignmentValues()
625
    {
626
        return array(
627
            array('contentobject_version', 1),
628
            array('from_node_id', 0),
629
            array('id', 215),
630
            array('is_main', 0),
631
            array('op_code', 3),
632
            array('parent_node', 77),
633
            array('parent_remote_id', 'some_id'),
634
            array('remote_id', '0'),
635
            array('sort_field', 2),
636
            array('sort_order', 0),
637
            array('is_main', 0),
638
            array('priority', 1),
639
            array('is_hidden', 1),
640
        );
641
    }
642
643
    /**
644
     * @depends testCreateLocation
645
     * @dataProvider getNodeAssignmentValues
646
     */
647 View Code Duplication
    public function testCreateLocationNodeAssignmentCreation($field, $value)
648
    {
649
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
650
        $handler = $this->getLocationGateway();
651
        $handler->createNodeAssignment(
652
            new CreateStruct(
653
                array(
654
                    'contentId' => 68,
655
                    'contentVersion' => 1,
656
                    'mainLocationId' => 1,
657
                    'priority' => 1,
658
                    'remoteId' => 'some_id',
659
                    'sortField' => 2,
660
                    'sortOrder' => 0,
661
                    'hidden' => 1,
662
                )
663
            ),
664
            '77',
665
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
666
        );
667
668
        $query = $this->handler->createSelectQuery();
669
        $this->assertQueryResult(
670
            array(array($value)),
671
            $query
672
                ->select($field)
673
                ->from('eznode_assignment')
674
                ->where(
675
                    $query->expr->lAnd(
676
                        $query->expr->eq('contentobject_id', 68),
677
                        $query->expr->eq('parent_node', 77)
678
                    )
679
                )
680
        );
681
    }
682
683
    /**
684
     * @depends testCreateLocation
685
     */
686 View Code Duplication
    public function testCreateLocationNodeAssignmentCreationMainLocation()
687
    {
688
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
689
        $handler = $this->getLocationGateway();
690
        $handler->createNodeAssignment(
691
            new CreateStruct(
692
                array(
693
                    'contentId' => 68,
694
                    'contentVersion' => 1,
695
                    'mainLocationId' => true,
696
                    'priority' => 1,
697
                    'remoteId' => 'some_id',
698
                    'sortField' => 1,
699
                    'sortOrder' => 1,
700
                )
701
            ),
702
            '77',
703
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
704
        );
705
706
        $query = $this->handler->createSelectQuery();
707
        $this->assertQueryResult(
708
            array(array(1)),
709
            $query
710
                ->select('is_main')
711
                ->from('eznode_assignment')
712
                ->where(
713
                    $query->expr->lAnd(
714
                        $query->expr->eq('contentobject_id', 68),
715
                        $query->expr->eq('parent_node', 77)
716
                    )
717
                )
718
        );
719
    }
720
721
    /**
722
     * @covers eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::updateLocationsContentVersionNo
723
     */
724
    public function testUpdateLocationsContentVersionNo()
725
    {
726
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
727
        $gateway = $this->getLocationGateway();
728
729
        $gateway->create(
730
            new CreateStruct(
731
                array(
732
                    'contentId' => 4096,
733
                    'remoteId' => 'some_id',
734
                    'contentVersion' => 1,
735
                )
736
            ),
737
            array(
738
                'node_id' => '77',
739
                'depth' => '2',
740
                'path_string' => '/1/2/77/',
741
            )
742
        );
743
744
        $gateway->updateLocationsContentVersionNo(4096, 2);
745
746
        $query = $this->handler->createSelectQuery();
747
        $this->assertQueryResult(
748
            array(
749
                array(2),
750
            ),
751
            $query->select(
752
                'contentobject_version'
753
            )->from(
754
                'ezcontentobject_tree'
755
            )->where(
756
                $query->expr->eq(
757
                    'contentobject_id',
758
                    4096
759
                )
760
            )
761
        );
762
    }
763
764
    /**
765
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::deleteNodeAssignment
766
     */
767
    public function testDeleteNodeAssignment()
768
    {
769
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
770
        $handler = $this->getLocationGateway();
771
772
        $handler->deleteNodeAssignment(11);
773
774
        $query = $this->handler->createSelectQuery();
775
        $this->assertQueryResult(
776
            array(array(0)),
777
            $query
778
                ->select('count(*)')
779
                ->from('eznode_assignment')
780
                ->where(
781
                    $query->expr->lAnd(
782
                        $query->expr->eq('contentobject_id', 11)
783
                    )
784
                )
785
        );
786
    }
787
788
    /**
789
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::deleteNodeAssignment
790
     */
791
    public function testDeleteNodeAssignmentWithSecondArgument()
792
    {
793
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
794
        $handler = $this->getLocationGateway();
795
796
        $query = $this->handler->createSelectQuery();
797
        $query
798
            ->select('count(*)')
799
            ->from('eznode_assignment')
800
            ->where(
801
                $query->expr->lAnd(
802
                    $query->expr->eq('contentobject_id', 11)
803
                )
804
            );
805
        $statement = $query->prepare();
806
        $statement->execute();
807
        $nodeAssignmentsCount = (int)$statement->fetchColumn();
808
809
        $handler->deleteNodeAssignment(11, 1);
810
811
        $query = $this->handler->createSelectQuery();
812
        $this->assertQueryResult(
813
            array(array($nodeAssignmentsCount - 1)),
814
            $query
815
                ->select('count(*)')
816
                ->from('eznode_assignment')
817
                ->where(
818
                    $query->expr->lAnd(
819
                        $query->expr->eq('contentobject_id', 11)
820
                    )
821
                )
822
        );
823
    }
824
825
    public static function getConvertNodeAssignmentsLocationValues()
826
    {
827
        return array(
828
            array('contentobject_id', '68'),
829
            array('contentobject_is_published', '1'),
830
            array('contentobject_version', '1'),
831
            array('depth', '3'),
832
            array('is_hidden', '1'),
833
            array('is_invisible', '1'),
834
            array('main_node_id', '70'),
835
            array('modified_subnode', time()),
836
            array('node_id', '228'),
837
            array('parent_node_id', '77'),
838
            array('path_identification_string', null),
839
            array('path_string', '/1/2/77/228/'),
840
            array('priority', '101'),
841
            array('remote_id', 'some_id'),
842
            array('sort_field', '1'),
843
            array('sort_order', '1'),
844
        );
845
    }
846
847
    /**
848
     * @depends testCreateLocationNodeAssignmentCreation
849
     * @dataProvider getConvertNodeAssignmentsLocationValues
850
     */
851
    public function testConvertNodeAssignments($field, $value)
852
    {
853
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
854
855
        $handler = $this->getLocationGateway();
856
        $handler->createNodeAssignment(
857
            new CreateStruct(
858
                array(
859
                    'contentId' => 68,
860
                    'contentVersion' => 1,
861
                    'mainLocationId' => false,
862
                    'priority' => 101,
863
                    'remoteId' => 'some_id',
864
                    'sortField' => 1,
865
                    'sortOrder' => 1,
866
                    'hidden' => true,
867
                    // Note: not stored in node assignment, will be calculated from parent
868
                    // visibility upon Location creation from node assignment
869
                    'invisible' => false,
870
                )
871
            ),
872
            '77',
873
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
874
        );
875
876
        $handler->createLocationsFromNodeAssignments(68, 1);
877
878
        $query = $this->handler->createSelectQuery();
879
        $query
880
            ->select($field)
881
            ->from('ezcontentobject_tree')
882
            ->where(
883
                $query->expr->lAnd(
884
                    $query->expr->eq('contentobject_id', 68),
885
                    $query->expr->eq('parent_node_id', 77)
886
                )
887
            );
888
889
        if ($field === 'modified_subnode') {
890
            $statement = $query->prepare();
891
            $statement->execute();
892
            $result = $statement->fetch(\PDO::FETCH_ASSOC);
893
            $this->assertGreaterThanOrEqual($value, $result);
894
        } else {
895
            $this->assertQueryResult(
896
                array(array($value)),
897
                $query
898
            );
899
        }
900
    }
901
902
    /**
903
     * @depends testCreateLocationNodeAssignmentCreation
904
     */
905 View Code Duplication
    public function testConvertNodeAssignmentsMainLocation()
906
    {
907
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
908
909
        $handler = $this->getLocationGateway();
910
        $handler->createNodeAssignment(
911
            new CreateStruct(
912
                array(
913
                    'contentId' => 68,
914
                    'contentVersion' => 1,
915
                    'mainLocationId' => true,
916
                    'priority' => 101,
917
                    'remoteId' => 'some_id',
918
                    'sortField' => 1,
919
                    'sortOrder' => 1,
920
                    'hidden' => true,
921
                    // Note: not stored in node assignment, will be calculated from parent
922
                    // visibility upon Location creation from node assignment
923
                    'invisible' => false,
924
                )
925
            ),
926
            '77',
927
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
928
        );
929
930
        $handler->createLocationsFromNodeAssignments(68, 1);
931
932
        $query = $this->handler->createSelectQuery();
933
        $this->assertQueryResult(
934
            array(array(228)),
935
            $query
936
                ->select('main_node_id')
937
                ->from('ezcontentobject_tree')
938
                ->where(
939
                    $query->expr->lAnd(
940
                        $query->expr->eq('contentobject_id', 68),
941
                        $query->expr->eq('parent_node_id', 77)
942
                    )
943
                )
944
        );
945
    }
946
947
    /**
948
     * @depends testCreateLocationNodeAssignmentCreation
949
     */
950 View Code Duplication
    public function testConvertNodeAssignmentsParentHidden()
951
    {
952
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
953
954
        $handler = $this->getLocationGateway();
955
        $handler->createNodeAssignment(
956
            new CreateStruct(
957
                array(
958
                    'contentId' => 68,
959
                    'contentVersion' => 1,
960
                    'mainLocationId' => true,
961
                    'priority' => 101,
962
                    'remoteId' => 'some_id',
963
                    'sortField' => 1,
964
                    'sortOrder' => 1,
965
                    'hidden' => false,
966
                    // Note: not stored in node assignment, will be calculated from parent
967
                    // visibility upon Location creation from node assignment
968
                    'invisible' => false,
969
                )
970
            ),
971
            '224',
972
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
973
        );
974
975
        $handler->createLocationsFromNodeAssignments(68, 1);
976
977
        $query = $this->handler->createSelectQuery();
978
        $this->assertQueryResult(
979
            array(array(0, 1)),
980
            $query
981
                ->select('is_hidden, is_invisible')
982
                ->from('ezcontentobject_tree')
983
                ->where(
984
                    $query->expr->lAnd(
985
                        $query->expr->eq('contentobject_id', 68),
986
                        $query->expr->eq('parent_node_id', 224)
987
                    )
988
                )
989
        );
990
    }
991
992
    /**
993
     * @depends testCreateLocationNodeAssignmentCreation
994
     */
995 View Code Duplication
    public function testConvertNodeAssignmentsParentInvisible()
996
    {
997
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
998
999
        $handler = $this->getLocationGateway();
1000
        $handler->createNodeAssignment(
1001
            new CreateStruct(
1002
                array(
1003
                    'contentId' => 68,
1004
                    'contentVersion' => 1,
1005
                    'mainLocationId' => true,
1006
                    'priority' => 101,
1007
                    'remoteId' => 'some_id',
1008
                    'sortField' => 1,
1009
                    'sortOrder' => 1,
1010
                    'hidden' => false,
1011
                    // Note: not stored in node assignment, will be calculated from parent
1012
                    // visibility upon Location creation from node assignment
1013
                    'invisible' => false,
1014
                )
1015
            ),
1016
            '225',
1017
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
1018
        );
1019
1020
        $handler->createLocationsFromNodeAssignments(68, 1);
1021
1022
        $query = $this->handler->createSelectQuery();
1023
        $this->assertQueryResult(
1024
            array(array(0, 1)),
1025
            $query
1026
                ->select('is_hidden, is_invisible')
1027
                ->from('ezcontentobject_tree')
1028
                ->where(
1029
                    $query->expr->lAnd(
1030
                        $query->expr->eq('contentobject_id', 68),
1031
                        $query->expr->eq('parent_node_id', 225)
1032
                    )
1033
                )
1034
        );
1035
    }
1036
1037
    /**
1038
     * @depends testCreateLocationNodeAssignmentCreation
1039
     */
1040 View Code Duplication
    public function testConvertNodeAssignmentsUpdateAssignment()
1041
    {
1042
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
1043
1044
        $handler = $this->getLocationGateway();
1045
        $handler->createNodeAssignment(
1046
            new CreateStruct(
1047
                array(
1048
                    'contentId' => 68,
1049
                    'contentVersion' => 1,
1050
                    'mainLocationId' => 1,
1051
                    'priority' => 1,
1052
                    'remoteId' => 'some_id',
1053
                    'sortField' => 1,
1054
                    'sortOrder' => 1,
1055
                )
1056
            ),
1057
            '77',
1058
            DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE
1059
        );
1060
1061
        $handler->createLocationsFromNodeAssignments(68, 1);
1062
1063
        $query = $this->handler->createSelectQuery();
1064
        $this->assertQueryResult(
1065
            array(array(DoctrineDatabase::NODE_ASSIGNMENT_OP_CODE_CREATE_NOP)),
1066
            $query
1067
                ->select('op_code')
1068
                ->from('eznode_assignment')
1069
                ->where(
1070
                    $query->expr->lAnd(
1071
                        $query->expr->eq('contentobject_id', 68),
1072
                        $query->expr->eq('parent_node', 77)
1073
                    )
1074
                )
1075
        );
1076
    }
1077
1078
    /**
1079
     * Test for the setSectionForSubtree() method.
1080
     *
1081
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::setSectionForSubtree
1082
     */
1083
    public function testSetSectionForSubtree()
1084
    {
1085
        $this->insertDatabaseFixture(__DIR__ . '/../../_fixtures/contentobjects.php');
1086
        $handler = $this->getLocationGateway();
1087
        $handler->setSectionForSubtree('/1/2/69/70/', 23);
1088
1089
        $query = $this->handler->createSelectQuery();
1090
        $this->assertQueryResult(
1091
            array(array(68), array(69)),
1092
            $query
1093
                ->select('id')
1094
                ->from('ezcontentobject')
1095
                ->where($query->expr->eq('section_id', 23))
1096
        );
1097
    }
1098
1099
    /**
1100
     * Test for the changeMainLocation() method.
1101
     *
1102
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::changeMainLocation
1103
     */
1104
    public function testChangeMainLocation()
1105
    {
1106
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
1107
        // Create additional location and assignment for test purpose
1108
        $query = $this->handler->createInsertQuery();
1109
        $query->insertInto($this->handler->quoteTable('ezcontentobject_tree'))
1110
            ->set($this->handler->quoteColumn('contentobject_id'), $query->bindValue(10, null, \PDO::PARAM_INT))
1111
            ->set($this->handler->quoteColumn('contentobject_version'), $query->bindValue(2, null, \PDO::PARAM_INT))
1112
            ->set($this->handler->quoteColumn('main_node_id'), $query->bindValue(15, null, \PDO::PARAM_INT))
1113
            ->set($this->handler->quoteColumn('node_id'), $query->bindValue(228, null, \PDO::PARAM_INT))
1114
            ->set($this->handler->quoteColumn('parent_node_id'), $query->bindValue(227, null, \PDO::PARAM_INT))
1115
            ->set($this->handler->quoteColumn('path_string'), $query->bindValue('/1/5/13/228/', null, \PDO::PARAM_STR))
1116
            ->set($this->handler->quoteColumn('remote_id'), $query->bindValue('asdfg123437', null, \PDO::PARAM_STR));
1117
        $query->prepare()->execute();
1118
        $query = $this->handler->createInsertQuery();
1119
        $query->insertInto($this->handler->quoteTable('eznode_assignment'))
1120
            ->set($this->handler->quoteColumn('contentobject_id'), $query->bindValue(10, null, \PDO::PARAM_INT))
1121
            ->set($this->handler->quoteColumn('contentobject_version'), $query->bindValue(2, null, \PDO::PARAM_INT))
1122
            ->set($this->handler->quoteColumn('id'), $query->bindValue(0, null, \PDO::PARAM_INT))
1123
            ->set($this->handler->quoteColumn('is_main'), $query->bindValue(0, null, \PDO::PARAM_INT))
1124
            ->set($this->handler->quoteColumn('parent_node'), $query->bindValue(227, null, \PDO::PARAM_INT))
1125
            ->set($this->handler->quoteColumn('parent_remote_id'), $query->bindValue('5238a276bf8231fbcf8a986cdc82a6a5', null, \PDO::PARAM_STR));
1126
        $query->prepare()->execute();
1127
1128
        $gateway = $this->getLocationGateway();
1129
1130
        $gateway->changeMainLocation(
1131
            10, // content id
1132
            228, // new main location id
1133
            2, // content version number
1134
            227 // new main location parent id
1135
        );
1136
1137
        $query = $this->handler->createSelectQuery();
1138
        $this->assertQueryResult(
1139
            array(array(228), array(228)),
1140
            $query->select(
1141
                'main_node_id'
1142
            )->from(
1143
                'ezcontentobject_tree'
1144
            )->where(
1145
                $query->expr->eq('contentobject_id', 10)
1146
            )
1147
        );
1148
1149
        $query = $this->handler->createSelectQuery();
1150
        $this->assertQueryResult(
1151
            array(array(1)),
1152
            $query->select(
1153
                'is_main'
1154
            )->from(
1155
                'eznode_assignment'
1156
            )->where(
1157
                $query->expr->lAnd(
1158
                    $query->expr->eq('contentobject_id', 10),
1159
                    $query->expr->eq('contentobject_version', 2),
1160
                    $query->expr->eq('parent_node', 227)
1161
                )
1162
            )
1163
        );
1164
1165
        $query = $this->handler->createSelectQuery();
1166
        $this->assertQueryResult(
1167
            array(array(0)),
1168
            $query->select(
1169
                'is_main'
1170
            )->from(
1171
                'eznode_assignment'
1172
            )->where(
1173
                $query->expr->lAnd(
1174
                    $query->expr->eq('contentobject_id', 10),
1175
                    $query->expr->eq('contentobject_version', 2),
1176
                    $query->expr->eq('parent_node', 44)
1177
                )
1178
            )
1179
        );
1180
    }
1181
1182
    /**
1183
     * Test for the getChildren() method.
1184
     *
1185
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::getChildren
1186
     */
1187
    public function testGetChildren()
1188
    {
1189
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
1190
1191
        $gateway = $this->getLocationGateway();
1192
        $childrenRows = $gateway->getChildren(213);
1193
1194
        $this->assertCount(2, $childrenRows);
1195
        $this->assertCount(16, $childrenRows[0]);
1196
        $this->assertEquals(214, $childrenRows[0]['node_id']);
1197
        $this->assertCount(16, $childrenRows[1]);
1198
        $this->assertEquals(215, $childrenRows[1]['node_id']);
1199
    }
1200
1201
    /**
1202
     * Test for the getFallbackMainNodeData() method.
1203
     *
1204
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::getFallbackMainNodeData
1205
     */
1206
    public function testGetFallbackMainNodeData()
1207
    {
1208
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
1209
        // Create additional location for test purpose
1210
        $query = $this->handler->createInsertQuery();
1211
        $query->insertInto($this->handler->quoteTable('ezcontentobject_tree'))
1212
            ->set($this->handler->quoteColumn('contentobject_id'), $query->bindValue(12, null, \PDO::PARAM_INT))
1213
            ->set($this->handler->quoteColumn('contentobject_version'), $query->bindValue(1, null, \PDO::PARAM_INT))
1214
            ->set($this->handler->quoteColumn('main_node_id'), $query->bindValue(13, null, \PDO::PARAM_INT))
1215
            ->set($this->handler->quoteColumn('node_id'), $query->bindValue(228, null, \PDO::PARAM_INT))
1216
            ->set($this->handler->quoteColumn('parent_node_id'), $query->bindValue(227, null, \PDO::PARAM_INT))
1217
            ->set($this->handler->quoteColumn('path_string'), $query->bindValue('/1/5/13/228/', null, \PDO::PARAM_STR))
1218
            ->set($this->handler->quoteColumn('remote_id'), $query->bindValue('asdfg123437', null, \PDO::PARAM_STR));
1219
        $query->prepare()->execute();
1220
1221
        $gateway = $this->getLocationGateway();
1222
        $data = $gateway->getFallbackMainNodeData(12, 13);
1223
1224
        $this->assertEquals(228, $data['node_id']);
1225
        $this->assertEquals(1, $data['contentobject_version']);
1226
        $this->assertEquals(227, $data['parent_node_id']);
1227
    }
1228
1229
    /**
1230
     * Test for the removeLocation() method.
1231
     *
1232
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::removeLocation
1233
     */
1234
    public function testRemoveLocation()
1235
    {
1236
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
1237
1238
        $gateway = $this->getLocationGateway();
1239
        $gateway->removeLocation(13);
1240
1241
        try {
1242
            $gateway->getBasicNodeData(13);
1243
            $this->fail('Location was not deleted!');
1244
        } catch (NotFoundException $e) {
1245
            // Do nothing
1246
        }
1247
    }
1248
1249
    public function providerForTestUpdatePathIdentificationString()
1250
    {
1251
        return array(
1252
            array(77, 2, 'new_solutions', 'new_solutions'),
1253
            array(75, 69, 'stylesheets', 'products/stylesheets'),
1254
        );
1255
    }
1256
1257
    /**
1258
     * Test for the updatePathIdentificationString() method.
1259
     *
1260
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::updatePathIdentificationString
1261
     * @dataProvider providerForTestUpdatePathIdentificationString
1262
     */
1263
    public function testUpdatePathIdentificationString($locationId, $parentLocationId, $text, $expected)
1264
    {
1265
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
1266
1267
        $gateway = $this->getLocationGateway();
1268
        $gateway->updatePathIdentificationString($locationId, $parentLocationId, $text);
1269
1270
        $query = $this->handler->createSelectQuery();
1271
        $this->assertQueryResult(
1272
            array(array($expected)),
1273
            $query->select(
1274
                'path_identification_string'
1275
            )->from(
1276
                'ezcontentobject_tree'
1277
            )->where(
1278
                $query->expr->eq('node_id', $locationId)
1279
            )
1280
        );
1281
    }
1282
}
1283