Completed
Push — ezp_30981_content_info_proxy ( 6fc040...a78a98 )
by
unknown
15:33
created

DoctrineDatabaseTrashTest::testCountTrashed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 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
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\Location\Gateway;
10
11
use eZ\Publish\Core\Persistence\Legacy\Tests\Content\LanguageAwareTestCase;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase;
13
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
14
use eZ\Publish\API\Repository\Values\Content\Query;
15
16
/**
17
 * Test case for eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase.
18
 */
19
class DoctrineDatabaseTrashTest extends LanguageAwareTestCase
20
{
21
    protected function getLocationGateway()
22
    {
23
        $dbHandler = $this->getDatabaseHandler();
24
25
        return new DoctrineDatabase(
26
            $dbHandler,
27
            $this->getLanguageMaskGenerator()
28
        );
29
    }
30
31
    /**
32
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::trashLocation
33
     *
34
     * @todo test updated content status
35
     */
36
    public function testTrashLocation()
37
    {
38
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
39
        $handler = $this->getLocationGateway();
40
        $handler->trashLocation(71);
41
42
        $query = $this->handler->createSelectQuery();
43
        $this->assertQueryResult(
44
            [
45
                [1, 0],
46
                [2, 0],
47
                [69, 0],
48
                [70, 0],
49
            ],
50
            $query
51
                ->select('node_id', 'priority')
52
                ->from('ezcontentobject_tree')
53
                ->where($query->expr->in('node_id', [1, 2, 69, 70, 71]))
54
        );
55
    }
56
57
    /**
58
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::trashLocation
59
     */
60 View Code Duplication
    public function testTrashLocationUpdateTrashTable()
61
    {
62
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
63
        $handler = $this->getLocationGateway();
64
        $handler->trashLocation(71);
65
66
        $query = $this->handler->createSelectQuery();
67
        $this->assertQueryResult(
68
            [
69
                [71, '/1/2/69/70/71/'],
70
            ],
71
            $query
72
                ->select('node_id', 'path_string')
73
                ->from('ezcontentobject_trash')
74
        );
75
    }
76
77 View Code Duplication
    public static function getUntrashedLocationValues()
78
    {
79
        return [
80
            ['contentobject_is_published', 1],
81
            ['contentobject_version', 1],
82
            ['depth', 4],
83
            ['is_hidden', 0],
84
            ['is_invisible', 0],
85
            ['main_node_id', 228],
86
            ['node_id', 228],
87
            ['parent_node_id', 70],
88
            ['path_identification_string', ''],
89
            ['path_string', '/1/2/69/70/228/'],
90
            ['priority', 0],
91
            ['remote_id', '087adb763245e0cdcac593fb4a5996cf'],
92
            ['sort_field', 1],
93
            ['sort_order', 1],
94
        ];
95
    }
96
97
    /**
98
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::untrashLocation
99
     * @dataProvider getUntrashedLocationValues
100
     */
101 View Code Duplication
    public function testUntrashLocationDefault($property, $value)
102
    {
103
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
104
        $handler = $this->getLocationGateway();
105
        $handler->trashLocation(71);
106
107
        $handler->untrashLocation(71);
108
109
        $query = $this->handler->createSelectQuery();
110
        $this->assertQueryResult(
111
            [[$value]],
112
            $query
113
                ->select($property)
114
                ->from('ezcontentobject_tree')
115
                ->where($query->expr->in('contentobject_id', [69]))
116
        );
117
    }
118
119
    /**
120
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::untrashLocation
121
     */
122 View Code Duplication
    public function testUntrashLocationNewParent()
123
    {
124
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
125
        $handler = $this->getLocationGateway();
126
        $handler->trashLocation(71);
127
128
        $handler->untrashLocation(71, 1);
129
130
        $query = $this->handler->createSelectQuery();
131
        $this->assertQueryResult(
132
            [['228', '1', '/1/228/']],
133
            $query
134
                ->select('node_id', 'parent_node_id', 'path_string')
135
                ->from('ezcontentobject_tree')
136
                ->where($query->expr->in('contentobject_id', [69]))
137
        );
138
    }
139
140
    /**
141
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::untrashLocation
142
     */
143 View Code Duplication
    public function testUntrashInvalidLocation()
144
    {
145
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
146
147
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
148
        $handler = $this->getLocationGateway();
149
150
        $handler->untrashLocation(23);
151
    }
152
153
    /**
154
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::untrashLocation
155
     */
156 View Code Duplication
    public function testUntrashLocationInvalidParent()
157
    {
158
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
159
160
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
161
        $handler = $this->getLocationGateway();
162
        $handler->trashLocation(71);
163
164
        $handler->untrashLocation(71, 1337);
165
    }
166
167
    /**
168
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::untrashLocation
169
     */
170 View Code Duplication
    public function testUntrashLocationInvalidOldParent()
171
    {
172
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
173
174
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
175
        $handler = $this->getLocationGateway();
176
        $handler->trashLocation(71);
177
        $handler->trashLocation(70);
178
179
        $handler->untrashLocation(70);
180
        $handler->untrashLocation(71);
181
    }
182
183 View Code Duplication
    public static function getLoadTrashValues()
184
    {
185
        return [
186
            ['node_id', 71],
187
            ['priority', 0],
188
            ['is_hidden', 0],
189
            ['is_invisible', 0],
190
            ['remote_id', '087adb763245e0cdcac593fb4a5996cf'],
191
            ['contentobject_id', 69],
192
            ['parent_node_id', 70],
193
            ['path_identification_string', 'products/software/os_type_i'],
194
            ['path_string', '/1/2/69/70/71/'],
195
            ['modified_subnode', 1311065013],
196
            ['main_node_id', 71],
197
            ['depth', 4],
198
            ['sort_field', 1],
199
            ['sort_order', 1],
200
        ];
201
    }
202
203
    /**
204
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::loadTrashByLocation
205
     * @dataProvider getLoadTrashValues
206
     */
207 View Code Duplication
    public function testLoadTrashByLocationId($field, $value)
208
    {
209
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
210
        $handler = $this->getLocationGateway();
211
        $handler->trashLocation(71);
212
213
        $data = $handler->loadTrashByLocation(71);
214
215
        $this->assertEquals(
216
            $value,
217
            $data[$field],
218
            "Value in property $field not as expected."
219
        );
220
    }
221
222
    /**
223
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::countTrashed
224
     */
225
    public function testCountTrashed()
226
    {
227
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
228
        $handler = $this->getLocationGateway();
229
230
        $this->assertEquals(
231
            0,
232
            $handler->countTrashed()
233
        );
234
235
        $this->trashSubtree();
236
237
        $this->assertEquals(
238
            8,
239
            $handler->countTrashed()
240
        );
241
    }
242
243
    /**
244
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::listTrashed
245
     */
246
    public function testListEmptyTrash()
247
    {
248
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
249
        $handler = $this->getLocationGateway();
250
251
        $this->assertEquals(
252
            [],
253
            $handler->listTrashed(0, null, [])
254
        );
255
    }
256
257
    protected function trashSubtree()
258
    {
259
        $handler = $this->getLocationGateway();
260
        $handler->trashLocation(69);
261
        $handler->trashLocation(70);
262
        $handler->trashLocation(71);
263
        $handler->trashLocation(72);
264
        $handler->trashLocation(73);
265
        $handler->trashLocation(74);
266
        $handler->trashLocation(75);
267
        $handler->trashLocation(76);
268
    }
269
270
    /**
271
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::listTrashed
272
     */
273 View Code Duplication
    public function testListFullTrash()
274
    {
275
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
276
        $handler = $this->getLocationGateway();
277
        $this->trashSubtree();
278
279
        $this->assertCount(
280
            8,
281
            $handler->listTrashed(0, null, [])
282
        );
283
    }
284
285
    /**
286
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::listTrashed
287
     */
288 View Code Duplication
    public function testListTrashLimited()
289
    {
290
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
291
        $handler = $this->getLocationGateway();
292
        $this->trashSubtree();
293
294
        $this->assertCount(
295
            5,
296
            $handler->listTrashed(0, 5, [])
297
        );
298
    }
299
300
    public static function getTrashValues()
301
    {
302
        return [
303
            ['contentobject_id', 67],
304
            ['contentobject_version', 1],
305
            ['depth', 2],
306
            ['is_hidden', 0],
307
            ['is_invisible', 0],
308
            ['main_node_id', 69],
309
            ['modified_subnode', 1311065014],
310
            ['node_id', 69],
311
            ['parent_node_id', 2],
312
            ['path_identification_string', 'products'],
313
            ['path_string', '/1/2/69/'],
314
            ['priority', 0],
315
            ['remote_id', '9cec85d730eec7578190ee95ce5a36f5'],
316
            ['sort_field', 2],
317
            ['sort_order', 1],
318
        ];
319
    }
320
321
    /**
322
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::listTrashed
323
     * @dataProvider getTrashValues
324
     */
325 View Code Duplication
    public function testListTrashItem($key, $value)
326
    {
327
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
328
        $handler = $this->getLocationGateway();
329
        $this->trashSubtree();
330
331
        $trashList = $handler->listTrashed(0, 1, []);
332
        $this->assertEquals($value, $trashList[0][$key]);
333
    }
334
335
    /**
336
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::listTrashed
337
     */
338 View Code Duplication
    public function testListTrashSortedPathStringDesc()
339
    {
340
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
341
        $handler = $this->getLocationGateway();
342
        $this->trashSubtree();
343
344
        $this->assertEquals(
345
            [
346
                '/1/2/69/76/',
347
                '/1/2/69/72/75/',
348
                '/1/2/69/72/74/',
349
                '/1/2/69/72/73/',
350
                '/1/2/69/72/',
351
                '/1/2/69/70/71/',
352
                '/1/2/69/70/',
353
                '/1/2/69/',
354
            ],
355
            array_map(
356
                function ($trashItem) {
357
                    return $trashItem['path_string'];
358
                },
359
                $trashList = $handler->listTrashed(
360
                    0,
361
                    null,
362
                    [
363
                        new SortClause\Location\Path(Query::SORT_DESC),
364
                    ]
365
                )
366
            )
367
        );
368
    }
369
370
    /**
371
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::listTrashed
372
     */
373 View Code Duplication
    public function testListTrashSortedDepth()
374
    {
375
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
376
        $handler = $this->getLocationGateway();
377
        $this->trashSubtree();
378
379
        $this->assertEquals(
380
            [
381
                '/1/2/69/',
382
                '/1/2/69/76/',
383
                '/1/2/69/72/',
384
                '/1/2/69/70/',
385
                '/1/2/69/72/75/',
386
                '/1/2/69/72/74/',
387
                '/1/2/69/72/73/',
388
                '/1/2/69/70/71/',
389
            ],
390
            array_map(
391
                function ($trashItem) {
392
                    return $trashItem['path_string'];
393
                },
394
                $trashList = $handler->listTrashed(
395
                    0,
396
                    null,
397
                    [
398
                        new SortClause\Location\Depth(),
399
                        new SortClause\Location\Path(Query::SORT_DESC),
400
                    ]
401
                )
402
            )
403
        );
404
    }
405
406
    /**
407
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::cleanupTrash
408
     */
409
    public function testCleanupTrash()
410
    {
411
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
412
        $handler = $this->getLocationGateway();
413
        $this->trashSubtree();
414
        $handler->cleanupTrash();
415
416
        $query = $this->handler->createSelectQuery();
417
        $this->assertQueryResult(
418
            [],
419
            $query
420
                ->select('*')
421
                ->from('ezcontentobject_trash')
422
        );
423
    }
424
425
    /**
426
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::removeElementFromTrash
427
     */
428
    public function testRemoveElementFromTrash()
429
    {
430
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
431
        $handler = $this->getLocationGateway();
432
        $this->trashSubtree();
433
        $handler->removeElementFromTrash(71);
434
435
        $query = $this->handler->createSelectQuery();
436
        $this->assertQueryResult(
437
            [],
438
            $query
439
                ->select('*')
440
                ->from('ezcontentobject_trash')
441
                ->where($query->expr->eq('node_id', 71))
442
        );
443
    }
444
445
    /**
446
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway\DoctrineDatabase::countLocationsByContentId
447
     */
448
    public function testCountLocationsByContentId()
449
    {
450
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/full_example_tree.php');
451
        $handler = $this->getLocationGateway();
452
453
        self::assertSame(0, $handler->countLocationsByContentId(123456789));
454
        self::assertSame(1, $handler->countLocationsByContentId(67));
455
456
        // Insert a new node and count again
457
        $query = $this->handler->createInsertQuery();
458
        $query
459
            ->insertInto('ezcontentobject_tree')
460
            ->set('contentobject_id', $query->bindValue(67, null, \PDO::PARAM_INT))
461
            ->set('contentobject_version', $query->bindValue(1, null, \PDO::PARAM_INT))
462
            ->set('path_string', $query->bindValue('/1/2/96'))
463
            ->set('parent_node_id', $query->bindValue(96, null, \PDO::PARAM_INT))
464
            ->set('remote_id', $query->bindValue('some_remote_id'));
465
        $query->prepare()->execute();
466
        self::assertSame(2, $handler->countLocationsByContentId(67));
467
    }
468
}
469