DoctrineDatabaseTest::getDatabaseGateway()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\Gateway;
8
9
use eZ\Publish\Core\Persistence\Legacy\Tests\Content\LanguageAwareTestCase;
10
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase;
11
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
12
use eZ\Publish\SPI\Persistence\Content;
13
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
14
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
15
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
16
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
17
use eZ\Publish\SPI\Persistence\Content\Field;
18
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
19
use eZ\Publish\API\Repository\Values\Content\Relation as RelationValue;
20
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
21
22
/**
23
 * Test case for eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase.
24
 */
25
class DoctrineDatabaseTest extends LanguageAwareTestCase
26
{
27
    /**
28
     * Database gateway to test.
29
     *
30
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase
31
     */
32
    protected $databaseGateway;
33
34
    /**
35
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::insertContentObject
36
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::generateLanguageMask
37
     *
38
     * @todo Fix not available fields
39
     */
40
    public function testInsertContentObject()
41
    {
42
        $struct = $this->getCreateStructFixture();
43
44
        $gateway = $this->getDatabaseGateway();
45
        $gateway->insertContentObject($struct);
46
47
        $this->assertQueryResult(
48
            [
49
                [
50
                    'name' => 'Content name',
51
                    'contentclass_id' => '23',
52
                    'section_id' => '42',
53
                    'owner_id' => '13',
54
                    'current_version' => '1',
55
                    'initial_language_id' => '2',
56
                    'remote_id' => 'some_remote_id',
57
                    'language_mask' => '3',
58
                    'modified' => '0',
59
                    'published' => '0',
60
                    'status' => ContentInfo::STATUS_DRAFT,
61
                ],
62
            ],
63
            $this->getDatabaseHandler()
64
                ->createSelectQuery()
65
                ->select(
66
                    [
67
                        'name',
68
                        'contentclass_id',
69
                        'section_id',
70
                        'owner_id',
71
                        'current_version',
72
                        'initial_language_id',
73
                        'remote_id',
74
                        'language_mask',
75
                        'modified',
76
                        'published',
77
                        'status',
78
                    ]
79
                )->from('ezcontentobject')
80
        );
81
    }
82
83
    /**
84
     * Returns a Content fixture.
85
     *
86
     * @return \eZ\Publish\SPI\Persistence\Content\CreateStruct
87
     */
88
    protected function getCreateStructFixture()
89
    {
90
        $struct = new CreateStruct();
91
92
        $struct->typeId = 23;
93
        $struct->sectionId = 42;
94
        $struct->ownerId = 13;
95
        $struct->initialLanguageId = 2;
96
        $struct->remoteId = 'some_remote_id';
97
        $struct->alwaysAvailable = true;
98
        $struct->modified = 456;
99
        $struct->name = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Content name') of type array<string,string,{"eng-US":"string"}> is incompatible with the declared type array<integer,string> of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
            'eng-US' => 'Content name',
101
        ];
102
        $struct->fields = [
103
            new Field(['languageCode' => 'eng-US']),
104
        ];
105
        $struct->locations = [];
106
107
        return $struct;
108
    }
109
110
    /**
111
     * Returns a Content fixture.
112
     *
113
     * @return \eZ\Publish\SPI\Persistence\Content
114
     */
115
    protected function getContentFixture()
116
    {
117
        $content = new Content();
118
119
        $content->versionInfo = new VersionInfo();
120
        $content->versionInfo->names = [
121
            'eng-US' => 'Content name',
122
        ];
123
        $content->versionInfo->status = VersionInfo::STATUS_PENDING;
124
125
        $content->versionInfo->contentInfo = new ContentInfo();
126
        $content->versionInfo->contentInfo->contentTypeId = 23;
127
        $content->versionInfo->contentInfo->sectionId = 42;
128
        $content->versionInfo->contentInfo->ownerId = 13;
129
        $content->versionInfo->contentInfo->currentVersionNo = 2;
130
        $content->versionInfo->contentInfo->mainLanguageCode = 'eng-US';
131
        $content->versionInfo->contentInfo->remoteId = 'some_remote_id';
132
        $content->versionInfo->contentInfo->alwaysAvailable = true;
133
        $content->versionInfo->contentInfo->publicationDate = 123;
134
        $content->versionInfo->contentInfo->modificationDate = 456;
135
        $content->versionInfo->contentInfo->isPublished = false;
136
        $content->versionInfo->contentInfo->name = 'Content name';
137
138
        return $content;
139
    }
140
141
    /**
142
     * Returns a Version fixture.
143
     *
144
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo
145
     */
146
    protected function getVersionFixture()
147
    {
148
        $version = new VersionInfo();
149
150
        $version->id = null;
151
        $version->versionNo = 1;
152
        $version->creatorId = 13;
153
        $version->status = 0;
154
        $version->creationDate = 1312278322;
155
        $version->modificationDate = 1312278323;
156
        $version->initialLanguageCode = 'eng-GB';
157
        $version->contentInfo = new ContentInfo(
158
            [
159
                'id' => 2342,
160
                'alwaysAvailable' => true,
161
            ]
162
        );
163
164
        return $version;
165
    }
166
167
    /**
168
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::insertVersion
169
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::generateLanguage
170
     */
171
    public function testInsertVersion()
172
    {
173
        $version = $this->getVersionFixture();
174
175
        $gateway = $this->getDatabaseGateway();
176
        $gateway->insertVersion($version, []);
177
178
        $this->assertQueryResult(
179
            [
180
                [
181
                    'contentobject_id' => '2342',
182
                    'created' => '1312278322',
183
                    'creator_id' => '13',
184
                    'modified' => '1312278323',
185
                    'status' => '0',
186
                    'workflow_event_pos' => '0',
187
                    'version' => '1',
188
                    'language_mask' => '5',
189
                    'initial_language_id' => '4',
190
                    // Not needed, according to field mapping document
191
                    // 'user_id',
192
                ],
193
            ],
194
            $this->getDatabaseHandler()
195
                ->createSelectQuery()
196
                ->select(
197
                    [
198
                        'contentobject_id',
199
                        'created',
200
                        'creator_id',
201
                        'modified',
202
                        'status',
203
                        'workflow_event_pos',
204
                        'version',
205
                        'language_mask',
206
                        'initial_language_id',
207
                    ]
208
                )->from('ezcontentobject_version')
209
        );
210
    }
211
212
    /**
213
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::setStatus
214
     */
215 View Code Duplication
    public function testSetStatus()
216
    {
217
        $gateway = $this->getDatabaseGateway();
218
219
        // insert content
220
        $struct = $this->getCreateStructFixture();
221
        $contentId = $gateway->insertContentObject($struct);
222
223
        // insert version
224
        $version = $this->getVersionFixture();
225
        $version->contentInfo->id = $contentId;
226
        $gateway->insertVersion($version, []);
227
228
        $this->assertTrue(
229
            $gateway->setStatus($version->contentInfo->id, $version->versionNo, VersionInfo::STATUS_PENDING)
230
        );
231
232
        $this->assertQueryResult(
233
            [[VersionInfo::STATUS_PENDING]],
234
            $this->getDatabaseHandler()
235
                ->createSelectQuery()
236
                ->select('status')
237
                ->from('ezcontentobject_version')
238
        );
239
240
        // check that content status has not been set to published
241
        $this->assertQueryResult(
242
            [[VersionInfo::STATUS_DRAFT]],
243
            $this->getDatabaseHandler()
244
                ->createSelectQuery()
245
                ->select('status')
246
                ->from('ezcontentobject')
247
        );
248
    }
249
250
    /**
251
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::setStatus
252
     */
253 View Code Duplication
    public function testSetStatusPublished()
254
    {
255
        $gateway = $this->getDatabaseGateway();
256
257
        // insert content
258
        $struct = $this->getCreateStructFixture();
259
        $contentId = $gateway->insertContentObject($struct);
260
261
        // insert version
262
        $version = $this->getVersionFixture();
263
        $version->contentInfo->id = $contentId;
264
        $gateway->insertVersion($version, []);
265
266
        $this->assertTrue(
267
            $gateway->setStatus($version->contentInfo->id, $version->versionNo, VersionInfo::STATUS_PUBLISHED)
268
        );
269
270
        $this->assertQueryResult(
271
            [[VersionInfo::STATUS_PUBLISHED]],
272
            $this->getDatabaseHandler()
273
                ->createSelectQuery()
274
                ->select('status')
275
                ->from('ezcontentobject_version')
276
        );
277
278
        // check that content status has been set to published
279
        $this->assertQueryResult(
280
            [[ContentInfo::STATUS_PUBLISHED]],
281
            $this->getDatabaseHandler()
282
                ->createSelectQuery()
283
                ->select('status')
284
                ->from('ezcontentobject')
285
        );
286
    }
287
288
    /**
289
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::setStatus
290
     */
291
    public function testSetStatusUnknownVersion()
292
    {
293
        $gateway = $this->getDatabaseGateway();
294
295
        $this->assertFalse(
296
            $gateway->setStatus(23, 42, 2)
297
        );
298
    }
299
300
    /**
301
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateContent
302
     */
303
    public function testUpdateContent()
304
    {
305
        $gateway = $this->getDatabaseGateway();
306
307
        $this->insertDatabaseFixture(
308
            __DIR__ . '/../_fixtures/contentobjects.php'
309
        );
310
311
        $metadataStruct = $this->getMetadataUpdateStructFixture();
312
313
        $gateway->updateContent(10, $metadataStruct);
314
315
        $this->assertQueryResult(
316
            [
317
                [
318
                    'initial_language_id' => '3',
319
                    'modified' => '234567',
320
                    'owner_id' => '42',
321
                    'published' => '123456',
322
                    'remote_id' => 'ghjk1234567890ghjk1234567890',
323
                    'name' => 'Thoth',
324
                ],
325
            ],
326
            $this->getDatabaseHandler()->createSelectQuery()
327
                ->select(
328
                    'initial_language_id',
329
                    'modified',
330
                    'owner_id',
331
                    'published',
332
                    'remote_id',
333
                    'name'
334
                )->from('ezcontentobject')
335
                ->where('id = 10')
336
        );
337
    }
338
339
    /**
340
     * Returns an UpdateStruct fixture.
341
     *
342
     * @return \eZ\Publish\SPI\Persistence\Content\UpdateStruct
343
     */
344
    protected function getUpdateStructFixture()
345
    {
346
        $struct = new UpdateStruct();
347
        $struct->creatorId = 23;
348
        $struct->fields = [];
349
        $struct->modificationDate = 234567;
350
        $struct->initialLanguageId = 2;
351
352
        return $struct;
353
    }
354
355
    /**
356
     * Returns a MetadataUpdateStruct fixture.
357
     *
358
     * @return \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct
359
     */
360
    protected function getMetadataUpdateStructFixture()
361
    {
362
        $struct = new MetadataUpdateStruct();
363
        $struct->ownerId = 42;
364
        $struct->publicationDate = 123456;
365
        $struct->mainLanguageId = 3;
366
        $struct->modificationDate = 234567;
367
        $struct->remoteId = 'ghjk1234567890ghjk1234567890';
368
        $struct->name = 'Thoth';
369
370
        return $struct;
371
    }
372
373
    /**
374
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateVersion
375
     */
376
    public function testUpdateVersion()
377
    {
378
        $gateway = $this->getDatabaseGateway();
379
380
        $this->insertDatabaseFixture(
381
            __DIR__ . '/../_fixtures/contentobjects.php'
382
        );
383
384
        $gateway->updateVersion(10, 2, $this->getUpdateStructFixture());
385
386
        $query = $this->getDatabaseHandler()->createSelectQuery();
387
        $this->assertQueryResult(
388
            [
389
                [
390
                    'creator_id' => '23',
391
                    'initial_language_id' => '2',
392
                    'modified' => '234567',
393
                ],
394
            ],
395
            $query
396
                ->select(
397
                    [
398
                        'creator_id',
399
                        'initial_language_id',
400
                        'modified',
401
                    ]
402
                )->from('ezcontentobject_version')
403
                ->where(
404
                    $query->expr->lAnd(
405
                        $query->expr->eq('contentobject_id', 10),
406
                        $query->expr->eq('version', 2)
407
                    )
408
                )
409
        );
410
    }
411
412
    /**
413
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::insertNewField
414
     */
415
    public function testInsertNewField()
416
    {
417
        $content = $this->getContentFixture();
418
        $content->versionInfo->contentInfo->id = 2342;
419
420
        $field = $this->getFieldFixture();
421
        $value = $this->getStorageValueFixture();
422
423
        $gateway = $this->getDatabaseGateway();
424
        $gateway->insertNewField($content, $field, $value);
425
426
        $this->assertQueryResult(
427
            [
428
                [
429
                    'contentclassattribute_id' => '231',
430
                    'contentobject_id' => '2342',
431
                    'data_float' => '24.42',
432
                    'data_int' => '42',
433
                    'data_text' => 'Test text',
434
                    'data_type_string' => 'ezstring',
435
                    'language_code' => 'eng-GB',
436
                    'language_id' => '4',
437
                    'sort_key_int' => '23',
438
                    'sort_key_string' => 'Test',
439
                    'version' => '1',
440
                ],
441
            ],
442
            $this->getDatabaseHandler()
443
                ->createSelectQuery()
444
                ->select(
445
                    [
446
                        'contentclassattribute_id',
447
                        'contentobject_id',
448
                        'data_float',
449
                        'data_int',
450
                        'data_text',
451
                        'data_type_string',
452
                        'language_code',
453
                        'language_id',
454
                        'sort_key_int',
455
                        'sort_key_string',
456
                        'version',
457
                    ]
458
                )->from('ezcontentobject_attribute')
459
        );
460
    }
461
462
    /**
463
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::insertNewField
464
     */
465
    public function testInsertNewAlwaysAvailableField()
466
    {
467
        $content = $this->getContentFixture();
468
        $content->versionInfo->contentInfo->id = 2342;
469
        // Set main language to the one used in the field fixture
470
        $content->versionInfo->contentInfo->mainLanguageCode = 'eng-GB';
471
472
        $field = $this->getFieldFixture();
473
        $value = $this->getStorageValueFixture();
474
475
        $gateway = $this->getDatabaseGateway();
476
        $gateway->insertNewField($content, $field, $value);
477
478
        $this->assertQueryResult(
479
            [
480
                [
481
                    'contentclassattribute_id' => '231',
482
                    'contentobject_id' => '2342',
483
                    'data_float' => '24.42',
484
                    'data_int' => '42',
485
                    'data_text' => 'Test text',
486
                    'data_type_string' => 'ezstring',
487
                    'language_code' => 'eng-GB',
488
                    'language_id' => '5',
489
                    'sort_key_int' => '23',
490
                    'sort_key_string' => 'Test',
491
                    'version' => '1',
492
                ],
493
            ],
494
            $this->getDatabaseHandler()
495
                ->createSelectQuery()
496
                ->select(
497
                    [
498
                        'contentclassattribute_id',
499
                        'contentobject_id',
500
                        'data_float',
501
                        'data_int',
502
                        'data_text',
503
                        'data_type_string',
504
                        'language_code',
505
                        'language_id',
506
                        'sort_key_int',
507
                        'sort_key_string',
508
                        'version',
509
                    ]
510
                )->from('ezcontentobject_attribute')
511
        );
512
    }
513
514
    /**
515
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateField
516
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::setFieldUpdateValues
517
     */
518
    public function testUpdateField()
519
    {
520
        $content = $this->getContentFixture();
521
        $content->versionInfo->contentInfo->id = 2342;
522
523
        $field = $this->getFieldFixture();
524
        $value = $this->getStorageValueFixture();
525
526
        $gateway = $this->getDatabaseGateway();
527
        $field->id = $gateway->insertNewField($content, $field, $value);
528
529
        $newValue = new StorageFieldValue(
530
            [
531
                'dataFloat' => 124.42,
532
                'dataInt' => 142,
533
                'dataText' => 'New text',
534
                'sortKeyInt' => 123,
535
                'sortKeyString' => 'new_text',
536
            ]
537
        );
538
539
        $gateway->updateField($field, $newValue);
540
541
        $this->assertQueryResult(
542
            [
543
                [
544
                    'data_float' => '124.42',
545
                    'data_int' => '142',
546
                    'data_text' => 'New text',
547
                    'sort_key_int' => '123',
548
                    'sort_key_string' => 'new_text',
549
                ],
550
            ],
551
            $this->getDatabaseHandler()
552
                ->createSelectQuery()
553
                ->select(
554
                    [
555
                        'data_float',
556
                        'data_int',
557
                        'data_text',
558
                        'sort_key_int',
559
                        'sort_key_string',
560
                    ]
561
                )->from('ezcontentobject_attribute')
562
        );
563
    }
564
565
    /**
566
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateNonTranslatableField
567
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::setFieldUpdateValues
568
     */
569
    public function testUpdateNonTranslatableField()
570
    {
571
        $content = $this->getContentFixture();
572
        $content->versionInfo->contentInfo->id = 2342;
573
574
        $fieldGb = $this->getFieldFixture();
575
        $fieldUs = $this->getOtherLanguageFieldFixture();
576
        $value = $this->getStorageValueFixture();
577
578
        $gateway = $this->getDatabaseGateway();
579
        $fieldGb->id = $gateway->insertNewField($content, $fieldGb, $value);
580
        $fieldUs->id = $gateway->insertNewField($content, $fieldUs, $value);
581
582
        $updateStruct = new Content\UpdateStruct();
0 ignored issues
show
Unused Code introduced by
$updateStruct 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...
583
584
        $newValue = new StorageFieldValue(
585
            [
586
                'dataFloat' => 124.42,
587
                'dataInt' => 142,
588
                'dataText' => 'New text',
589
                'sortKeyInt' => 123,
590
                'sortKeyString' => 'new_text',
591
            ]
592
        );
593
594
        $gateway->updateNonTranslatableField($fieldGb, $newValue, $content->versionInfo->contentInfo->id);
595
596
        $this->assertQueryResult(
597
            [
598
                // Both fields updated
599
                [
600
                    'data_float' => '124.42',
601
                    'data_int' => '142',
602
                    'data_text' => 'New text',
603
                    'sort_key_int' => '123',
604
                    'sort_key_string' => 'new_text',
605
                ],
606
                [
607
                    'data_float' => '124.42',
608
                    'data_int' => '142',
609
                    'data_text' => 'New text',
610
                    'sort_key_int' => '123',
611
                    'sort_key_string' => 'new_text',
612
                ],
613
            ],
614
            $this->getDatabaseHandler()
615
                ->createSelectQuery()
616
                ->select(
617
                    [
618
                        'data_float',
619
                        'data_int',
620
                        'data_text',
621
                        'sort_key_int',
622
                        'sort_key_string',
623
                    ]
624
                )->from('ezcontentobject_attribute')
625
        );
626
    }
627
628
    /**
629
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::listVersions
630
     */
631
    public function testListVersions(): void
632
    {
633
        $this->insertDatabaseFixture(
634
            __DIR__ . '/../_fixtures/contentobjects.php'
635
        );
636
637
        $gateway = $this->getDatabaseGateway();
638
        $res = $gateway->listVersions(226);
639
640
        $this->assertCount(
641
            2,
642
            $res
643
        );
644
645
        foreach ($res as $row) {
646
            $this->assertCount(
647
                23,
648
                $row
649
            );
650
        }
651
652
        $this->assertEquals(
653
            675,
654
            $res[0]['ezcontentobject_version_id']
655
        );
656
        $this->assertEquals(
657
            676,
658
            $res[1]['ezcontentobject_version_id']
659
        );
660
    }
661
662
    /**
663
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::listVersionNumbers
664
     */
665 View Code Duplication
    public function testListVersionNumbers()
666
    {
667
        $this->insertDatabaseFixture(
668
            __DIR__ . '/../_fixtures/contentobjects.php'
669
        );
670
671
        $gateway = $this->getDatabaseGateway();
672
        $res = $gateway->listVersionNumbers(226);
673
674
        $this->assertEquals([1, 2], $res);
675
    }
676
677
    /**
678
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::listVersionsForUser
679
     */
680
    public function testListVersionsForUser()
681
    {
682
        $this->insertDatabaseFixture(
683
            __DIR__ . '/../_fixtures/contentobjects.php'
684
        );
685
686
        $gateway = $this->getDatabaseGateway();
687
        $res = $gateway->listVersionsForUser(14);
688
689
        $this->assertCount(
690
            2,
691
            $res
692
        );
693
694
        foreach ($res as $row) {
695
            $this->assertCount(
696
                23,
697
                $row
698
            );
699
        }
700
701
        $this->assertEquals(
702
            677,
703
            $res[0]['ezcontentobject_version_id']
704
        );
705
        $this->assertEquals(
706
            0,
707
            $res[0]['ezcontentobject_version_status']
708
        );
709
        $this->assertEquals(
710
            678,
711
            $res[1]['ezcontentobject_version_id']
712
        );
713
        $this->assertEquals(
714
            0,
715
            $res[1]['ezcontentobject_version_status']
716
        );
717
    }
718
719
    /**
720
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::load
721
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
722
     */
723
    public function testLoadWithAllTranslations()
724
    {
725
        $this->insertDatabaseFixture(
726
            __DIR__ . '/../_fixtures/contentobjects.php'
727
        );
728
729
        $gateway = $this->getDatabaseGateway();
730
        $res = $gateway->load(226, 2);
731
732
        $this->assertValuesInRows(
733
            'ezcontentobject_attribute_language_code',
734
            ['eng-US', 'eng-GB'],
735
            $res
736
        );
737
738
        $this->assertValuesInRows(
739
            'ezcontentobject_attribute_language_id',
740
            ['2', '4'],
741
            $res
742
        );
743
    }
744
745
    /**
746
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::load
747
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
748
     */
749 View Code Duplication
    public function testCreateFixtureForMapperExtractContentFromRowsMultipleVersions()
750
    {
751
        $this->insertDatabaseFixture(
752
            __DIR__ . '/../_fixtures/contentobjects.php'
753
        );
754
755
        $gateway = $this->getDatabaseGateway();
756
757
        $resFirst = $gateway->load(11, 1);
758
        $resSecond = $gateway->load(11, 2);
759
760
        $res = array_merge($resFirst, $resSecond);
761
762
        $orig = include __DIR__ . '/../_fixtures/extract_content_from_rows_multiple_versions.php';
763
764
        /*$this->storeFixture(
765
            __DIR__ . '/../_fixtures/extract_content_from_rows_multiple_versions.php',
766
            $res
767
        );*/
768
769
        $this->assertEquals($orig, $res, 'Fixtures differ between what was previously stored(expected) and what it now generates(actual), this hints either some mistake in impl or that the fixture (../_fixtures/extract_content_from_rows_multiple_versions.php) and tests needs to be adapted.');
770
    }
771
772
    /**
773
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::load
774
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
775
     */
776
    public function testCreateFixtureForMapperExtractContentFromRows()
777
    {
778
        $this->insertDatabaseFixture(
779
            __DIR__ . '/../_fixtures/contentobjects.php'
780
        );
781
782
        $gateway = $this->getDatabaseGateway();
783
784
        $res = array_merge($gateway->load(226, 2));
785
786
        $orig = include __DIR__ . '/../_fixtures/extract_content_from_rows.php';
787
788
        /*$this->storeFixture(
789
            __DIR__ . '/../_fixtures/extract_content_from_rows.php',
790
            $res
791
        );*/
792
793
        $this->assertEquals($orig, $res, 'Fixtures differ between what was previously stored(expected) and what it now generates(actual), this hints either some mistake in impl or that the fixture (../_fixtures/extract_content_from_rows.php) and tests needs to be adapted.');
794
    }
795
796
    /**
797
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::load
798
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
799
     */
800 View Code Duplication
    public function testLoadWithSingleTranslation()
801
    {
802
        $this->insertDatabaseFixture(
803
            __DIR__ . '/../_fixtures/contentobjects.php'
804
        );
805
806
        $gateway = $this->getDatabaseGateway();
807
        $res = $gateway->load(226, 2, ['eng-GB']);
808
809
        $this->assertValuesInRows(
810
            'ezcontentobject_attribute_language_code',
811
            ['eng-GB'],
812
            $res
813
        );
814
        $this->assertValuesInRows(
815
            'ezcontentobject_attribute_language_id',
816
            ['4'],
817
            $res
818
        );
819
        $this->assertCount(
820
            1,
821
            $res
822
        );
823
    }
824
825
    /**
826
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::load
827
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
828
     */
829 View Code Duplication
    public function testLoadNonExistentTranslation()
830
    {
831
        $this->insertDatabaseFixture(
832
            __DIR__ . '/../_fixtures/contentobjects.php'
833
        );
834
835
        $gateway = $this->getDatabaseGateway();
836
        $res = $gateway->load(226, 2, ['de-DE']);
837
838
        $this->assertCount(
839
            0,
840
            $res
841
        );
842
    }
843
844
    /**
845
     * Asserts that $columnKey in $actualRows exactly contains $expectedValues.
846
     *
847
     * @param string $columnKey
848
     * @param string[] $expectedValues
849
     * @param string[][] $actualRows
850
     */
851
    protected function assertValuesInRows($columnKey, array $expectedValues, array $actualRows)
852
    {
853
        $expectedValues = array_fill_keys(
854
            array_values($expectedValues),
855
            true
856
        );
857
858
        $containedValues = [];
859
860
        foreach ($actualRows as $row) {
861
            if (isset($row[$columnKey])) {
862
                $containedValues[$row[$columnKey]] = true;
863
            }
864
        }
865
866
        $this->assertEquals(
867
            $expectedValues,
868
            $containedValues
869
        );
870
    }
871
872
    /**
873
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::getAllLocationIds
874
     */
875 View Code Duplication
    public function testGetAllLocationIds()
876
    {
877
        $this->insertDatabaseFixture(
878
            __DIR__ . '/../_fixtures/contentobjects.php'
879
        );
880
881
        $gateway = $this->getDatabaseGateway();
882
883
        $this->assertEquals(
884
            [228],
885
            $gateway->getAllLocationIds(226)
886
        );
887
    }
888
889
    /**
890
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::getFieldIdsByType
891
     */
892
    public function testGetFieldIdsByType()
893
    {
894
        $this->insertDatabaseFixture(
895
            __DIR__ . '/../_fixtures/contentobjects.php'
896
        );
897
898
        $gateway = $this->getDatabaseGateway();
899
900
        $this->assertEquals(
901
            [
902
                'ezstring' => [841],
903
                'ezimage' => [843],
904
                'ezkeyword' => [844],
905
            ],
906
            $gateway->getFieldIdsByType(149)
907
        );
908
    }
909
910
    /**
911
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::getFieldIdsByType
912
     */
913
    public function testGetFieldIdsByTypeWithSecondArgument()
914
    {
915
        $this->insertDatabaseFixture(
916
            __DIR__ . '/../_fixtures/contentobjects.php'
917
        );
918
919
        $gateway = $this->getDatabaseGateway();
920
921
        $this->assertEquals(
922
            [
923
                'ezstring' => [4001, 4002],
924
            ],
925
            $gateway->getFieldIdsByType(225, 2)
926
        );
927
    }
928
929
    /**
930
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteRelations
931
     */
932 View Code Duplication
    public function testDeleteRelationsTo()
933
    {
934
        $this->insertDatabaseFixture(
935
            __DIR__ . '/../_fixtures/contentobjects.php'
936
        );
937
938
        $beforeCount = [
939
            'all' => $this->countContentRelations(),
940
            'from' => $this->countContentRelations(149),
941
            'to' => $this->countContentRelations(null, 149),
942
        ];
943
944
        $gateway = $this->getDatabaseGateway();
945
        $gateway->deleteRelations(149);
946
947
        $this->assertEquals(
948
            // yes, relates to itself!
949
            [
950
                'all' => $beforeCount['all'] - 2,
951
                'from' => $beforeCount['from'] - 1,
952
                'to' => $beforeCount['to'] - 2,
953
            ],
954
            [
955
                'all' => $this->countContentRelations(),
956
                'from' => $this->countContentRelations(149),
957
                'to' => $this->countContentRelations(null, 149),
958
            ]
959
        );
960
    }
961
962
    /**
963
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteRelations
964
     */
965 View Code Duplication
    public function testDeleteRelationsFrom()
966
    {
967
        $this->insertDatabaseFixture(
968
            __DIR__ . '/../_fixtures/contentobjects.php'
969
        );
970
971
        $beforeCount = [
972
            'all' => $this->countContentRelations(),
973
            'from' => $this->countContentRelations(75),
974
            'to' => $this->countContentRelations(null, 75),
975
        ];
976
977
        $gateway = $this->getDatabaseGateway();
978
        $gateway->deleteRelations(75);
979
980
        $this->assertEquals(
981
            [
982
                'all' => $beforeCount['all'] - 6,
983
                'from' => $beforeCount['from'] - 6,
984
                'to' => $beforeCount['to'],
985
            ],
986
            [
987
                'all' => $this->countContentRelations(),
988
                'from' => $this->countContentRelations(75),
989
                'to' => $this->countContentRelations(null, 75),
990
            ]
991
        );
992
    }
993
994
    /**
995
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteRelations
996
     */
997 View Code Duplication
    public function testDeleteRelationsWithSecondArgument()
998
    {
999
        $this->insertDatabaseFixture(
1000
            __DIR__ . '/../_fixtures/contentobjects.php'
1001
        );
1002
1003
        $beforeCount = [
1004
            'all' => $this->countContentRelations(),
1005
            'from' => $this->countContentRelations(225),
1006
            'to' => $this->countContentRelations(null, 225),
1007
        ];
1008
1009
        $gateway = $this->getDatabaseGateway();
1010
        $gateway->deleteRelations(225, 2);
1011
1012
        $this->assertEquals(
1013
            [
1014
                'all' => $beforeCount['all'] - 1,
1015
                'from' => $beforeCount['from'] - 1,
1016
                'to' => $beforeCount['to'],
1017
            ],
1018
            [
1019
                'all' => $this->countContentRelations(),
1020
                'from' => $this->countContentRelations(225),
1021
                'to' => $this->countContentRelations(null, 225),
1022
            ]
1023
        );
1024
    }
1025
1026
    /**
1027
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteField
1028
     */
1029
    public function testDeleteField()
1030
    {
1031
        $this->insertDatabaseFixture(
1032
            __DIR__ . '/../_fixtures/contentobjects.php'
1033
        );
1034
1035
        $beforeCount = $this->countContentFields();
1036
1037
        $gateway = $this->getDatabaseGateway();
1038
        $gateway->deleteField(22);
1039
1040
        $this->assertEquals(
1041
            $beforeCount - 2,
1042
            $this->countContentFields()
1043
        );
1044
1045
        $this->assertQueryResult(
1046
            [],
1047
            $this->getDatabaseHandler()->createSelectQuery()
1048
                ->select('*')
1049
                ->from('ezcontentobject_attribute')
1050
                ->where('id=22')
1051
        );
1052
    }
1053
1054
    /**
1055
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteFields
1056
     */
1057 View Code Duplication
    public function testDeleteFields()
1058
    {
1059
        $this->insertDatabaseFixture(
1060
            __DIR__ . '/../_fixtures/contentobjects.php'
1061
        );
1062
1063
        $beforeCount = [
1064
            'all' => $this->countContentFields(),
1065
            'this' => $this->countContentFields(4),
1066
        ];
1067
1068
        $gateway = $this->getDatabaseGateway();
1069
        $gateway->deleteFields(4);
1070
1071
        $this->assertEquals(
1072
            [
1073
                'all' => $beforeCount['all'] - 2,
1074
                'this' => 0,
1075
            ],
1076
            [
1077
                'all' => $this->countContentFields(),
1078
                'this' => $this->countContentFields(4),
1079
            ]
1080
        );
1081
    }
1082
1083
    /**
1084
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteFields
1085
     */
1086 View Code Duplication
    public function testDeleteFieldsWithSecondArgument()
1087
    {
1088
        $this->insertDatabaseFixture(
1089
            __DIR__ . '/../_fixtures/contentobjects.php'
1090
        );
1091
1092
        $beforeCount = [
1093
            'all' => $this->countContentFields(),
1094
            'this' => $this->countContentFields(225),
1095
        ];
1096
1097
        $gateway = $this->getDatabaseGateway();
1098
        $gateway->deleteFields(225, 2);
1099
1100
        $this->assertEquals(
1101
            [
1102
                'all' => $beforeCount['all'] - 2,
1103
                'this' => $beforeCount['this'] - 2,
1104
            ],
1105
            [
1106
                'all' => $this->countContentFields(),
1107
                'this' => $this->countContentFields(225),
1108
            ]
1109
        );
1110
    }
1111
1112
    /**
1113
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteVersions
1114
     */
1115 View Code Duplication
    public function testDeleteVersions()
1116
    {
1117
        $this->insertDatabaseFixture(
1118
            __DIR__ . '/../_fixtures/contentobjects.php'
1119
        );
1120
1121
        $beforeCount = [
1122
            'all' => $this->countContentVersions(),
1123
            'this' => $this->countContentVersions(14),
1124
        ];
1125
1126
        $gateway = $this->getDatabaseGateway();
1127
        $gateway->deleteVersions(14);
1128
1129
        $this->assertEquals(
1130
            [
1131
                'all' => $beforeCount['all'] - 2,
1132
                'this' => 0,
1133
            ],
1134
            [
1135
                'all' => $this->countContentVersions(),
1136
                'this' => $this->countContentVersions(14),
1137
            ]
1138
        );
1139
    }
1140
1141
    /**
1142
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteVersions
1143
     */
1144 View Code Duplication
    public function testDeleteVersionsWithSecondArgument()
1145
    {
1146
        $this->insertDatabaseFixture(
1147
            __DIR__ . '/../_fixtures/contentobjects.php'
1148
        );
1149
1150
        $beforeCount = [
1151
            'all' => $this->countContentVersions(),
1152
            'this' => $this->countContentVersions(225),
1153
        ];
1154
1155
        $gateway = $this->getDatabaseGateway();
1156
        $gateway->deleteVersions(225, 2);
1157
1158
        $this->assertEquals(
1159
            [
1160
                'all' => $beforeCount['all'] - 1,
1161
                'this' => $beforeCount['this'] - 1,
1162
            ],
1163
            [
1164
                'all' => $this->countContentVersions(),
1165
                'this' => $this->countContentVersions(225),
1166
            ]
1167
        );
1168
    }
1169
1170
    /**
1171
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::setName
1172
     *
1173
     * @throws \Exception
1174
     */
1175
    public function testSetName()
1176
    {
1177
        $this->insertDatabaseFixture(
1178
            __DIR__ . '/../_fixtures/contentobjects.php'
1179
        );
1180
1181
        $gateway = $this->getDatabaseGateway();
1182
1183
        $gateway->setName(14, 2, 'Hello world!', 'eng-GB');
1184
1185
        $query = $this->getDatabaseHandler()->createSelectQuery();
1186
        $this->assertQueryResult(
1187
            [['eng-GB', 2, 14, 4, 'Hello world!', 'eng-GB']],
1188
            $query
1189
                ->select(
1190
                    [
1191
                        'content_translation',
1192
                        'content_version',
1193
                        'contentobject_id',
1194
                        'language_id',
1195
                        'name',
1196
                        'real_translation',
1197
                    ]
1198
                )
1199
                ->from('ezcontentobject_name')
1200
                ->where(
1201
                    $query->expr->lAnd(
1202
                        $query->expr->eq('contentobject_id', 14),
1203
                        $query->expr->eq('content_version', 2),
1204
                        $query->expr->eq('content_translation', $query->bindValue('eng-GB'))
1205
                    )
1206
                )
1207
        );
1208
    }
1209
1210
    /**
1211
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteNames
1212
     */
1213 View Code Duplication
    public function testDeleteNames()
1214
    {
1215
        $this->insertDatabaseFixture(
1216
            __DIR__ . '/../_fixtures/contentobjects.php'
1217
        );
1218
1219
        $beforeCount = [
1220
            'all' => $this->countContentNames(),
1221
            'this' => $this->countContentNames(14),
1222
        ];
1223
1224
        $gateway = $this->getDatabaseGateway();
1225
        $gateway->deleteNames(14);
1226
1227
        $this->assertEquals(
1228
            [
1229
                'all' => $beforeCount['all'] - 2,
1230
                'this' => 0,
1231
            ],
1232
            [
1233
                'all' => $this->countContentNames(),
1234
                'this' => $this->countContentNames(14),
1235
            ]
1236
        );
1237
    }
1238
1239
    /**
1240
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteNames
1241
     */
1242 View Code Duplication
    public function testDeleteNamesWithSecondArgument()
1243
    {
1244
        $this->insertDatabaseFixture(
1245
            __DIR__ . '/../_fixtures/contentobjects.php'
1246
        );
1247
1248
        $beforeCount = [
1249
            'all' => $this->countContentNames(),
1250
            'this' => $this->countContentNames(225),
1251
        ];
1252
1253
        $gateway = $this->getDatabaseGateway();
1254
        $gateway->deleteNames(225, 2);
1255
1256
        $this->assertEquals(
1257
            [
1258
                'all' => $beforeCount['all'] - 1,
1259
                'this' => $beforeCount['this'] - 1,
1260
            ],
1261
            [
1262
                'all' => $this->countContentNames(),
1263
                'this' => $this->countContentNames(225),
1264
            ]
1265
        );
1266
    }
1267
1268
    /**
1269
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteContent
1270
     */
1271
    public function testDeleteContent()
1272
    {
1273
        $this->insertDatabaseFixture(
1274
            __DIR__ . '/../_fixtures/contentobjects.php'
1275
        );
1276
1277
        $beforeCount = $this->countContent();
1278
1279
        $gateway = $this->getDatabaseGateway();
1280
        $gateway->deleteContent(14);
1281
1282
        $this->assertEquals(
1283
            [
1284
                'all' => $beforeCount - 1,
1285
                'this' => 0,
1286
            ],
1287
            [
1288
                'all' => $this->countContent(),
1289
                'this' => $this->countContent(14),
1290
            ]
1291
        );
1292
    }
1293
1294
    /**
1295
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::loadRelations
1296
     *
1297
     * @throws \Doctrine\DBAL\DBALException
1298
     */
1299 View Code Duplication
    public function testLoadRelations(): void
1300
    {
1301
        $this->insertRelationFixture();
1302
1303
        $gateway = $this->getDatabaseGateway();
1304
1305
        $relations = $gateway->loadRelations(57);
1306
1307
        $this->assertCount(3, $relations);
1308
1309
        $this->assertValuesInRows(
1310
            'ezcontentobject_link_to_contentobject_id',
1311
            [58, 59, 60],
1312
            $relations
1313
        );
1314
1315
        $this->assertValuesInRows(
1316
            'ezcontentobject_link_from_contentobject_id',
1317
            [57],
1318
            $relations
1319
        );
1320
        $this->assertValuesInRows(
1321
            'ezcontentobject_link_from_contentobject_version',
1322
            [2],
1323
            $relations
1324
        );
1325
    }
1326
1327
    /**
1328
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::loadRelations
1329
     */
1330 View Code Duplication
    public function testLoadRelationsByType()
1331
    {
1332
        $this->insertRelationFixture();
1333
1334
        $gateway = $this->getDatabaseGateway();
1335
1336
        $relations = $gateway->loadRelations(57, null, \eZ\Publish\API\Repository\Values\Content\Relation::COMMON);
1337
1338
        $this->assertCount(1, $relations, 'Expecting one relation to be loaded');
1339
1340
        $this->assertValuesInRows(
1341
            'ezcontentobject_link_relation_type',
1342
            [\eZ\Publish\API\Repository\Values\Content\Relation::COMMON],
1343
            $relations
1344
        );
1345
1346
        $this->assertValuesInRows(
1347
            'ezcontentobject_link_to_contentobject_id',
1348
            [58],
1349
            $relations
1350
        );
1351
    }
1352
1353
    /**
1354
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::loadRelations
1355
     */
1356 View Code Duplication
    public function testLoadRelationsByVersion()
1357
    {
1358
        $this->insertRelationFixture();
1359
1360
        $gateway = $this->getDatabaseGateway();
1361
1362
        $relations = $gateway->loadRelations(57, 1);
1363
1364
        $this->assertCount(1, $relations, 'Expecting one relation to be loaded');
1365
1366
        $this->assertValuesInRows(
1367
            'ezcontentobject_link_to_contentobject_id',
1368
            [58],
1369
            $relations
1370
        );
1371
    }
1372
1373
    /**
1374
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::loadRelations
1375
     */
1376
    public function testLoadRelationsNoResult()
1377
    {
1378
        $this->insertRelationFixture();
1379
1380
        $gateway = $this->getDatabaseGateway();
1381
1382
        $relations = $gateway->loadRelations(57, 1, \eZ\Publish\API\Repository\Values\Content\Relation::EMBED);
1383
1384
        $this->assertCount(0, $relations, 'Expecting no relation to be loaded');
1385
    }
1386
1387
    /**
1388
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::loadReverseRelations
1389
     */
1390 View Code Duplication
    public function testLoadReverseRelations()
1391
    {
1392
        $this->insertRelationFixture();
1393
1394
        $gateway = $this->getDatabaseGateway();
1395
1396
        $relations = $gateway->loadReverseRelations(58);
1397
1398
        self::assertCount(2, $relations);
1399
1400
        $this->assertValuesInRows(
1401
            'ezcontentobject_link_from_contentobject_id',
1402
            [57, 61],
1403
            $relations
1404
        );
1405
    }
1406
1407
    /**
1408
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::loadReverseRelations
1409
     */
1410 View Code Duplication
    public function testLoadReverseRelationsWithType()
1411
    {
1412
        $this->insertRelationFixture();
1413
1414
        $gateway = $this->getDatabaseGateway();
1415
1416
        $relations = $gateway->loadReverseRelations(58, \eZ\Publish\API\Repository\Values\Content\Relation::COMMON);
1417
1418
        self::assertCount(1, $relations);
1419
1420
        $this->assertValuesInRows(
1421
            'ezcontentobject_link_from_contentobject_id',
1422
            [57],
1423
            $relations
1424
        );
1425
1426
        $this->assertValuesInRows(
1427
            'ezcontentobject_link_relation_type',
1428
            [\eZ\Publish\API\Repository\Values\Content\Relation::COMMON],
1429
            $relations
1430
        );
1431
    }
1432
1433
    /**
1434
     * Inserts the relation database fixture from relation_data.php.
1435
     */
1436
    protected function insertRelationFixture()
1437
    {
1438
        $this->insertDatabaseFixture(
1439
            __DIR__ . '/../_fixtures/relations_data.php'
1440
        );
1441
    }
1442
1443
    /*
1444
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::getLastVersionNumber
1445
     *
1446
     * @return void
1447
     */
1448
    public function testGetLastVersionNumber()
1449
    {
1450
        $this->insertDatabaseFixture(
1451
            __DIR__ . '/../_fixtures/contentobjects.php'
1452
        );
1453
1454
        $gateway = $this->getDatabaseGateway();
1455
1456
        $this->assertEquals(
1457
            1,
1458
            $gateway->getLastVersionNumber(4)
1459
        );
1460
    }
1461
1462
    /**
1463
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::insertRelation
1464
     */
1465
    public function testInsertRelation()
1466
    {
1467
        $struct = $this->getRelationCreateStructFixture();
1468
        $gateway = $this->getDatabaseGateway();
1469
        $gateway->insertRelation($struct);
1470
1471
        $this->assertQueryResult(
1472
            [
1473
                [
1474
                    'id' => 1,
1475
                    'from_contentobject_id' => $struct->sourceContentId,
1476
                    'from_contentobject_version' => $struct->sourceContentVersionNo,
1477
                    'contentclassattribute_id' => $struct->sourceFieldDefinitionId,
1478
                    'to_contentobject_id' => $struct->destinationContentId,
1479
                    'relation_type' => $struct->type,
1480
                ],
1481
            ],
1482
            $this->getDatabaseHandler()
1483
                ->createSelectQuery()
1484
                ->select(
1485
                    [
1486
                        'id',
1487
                        'from_contentobject_id',
1488
                        'from_contentobject_version',
1489
                        'contentclassattribute_id',
1490
                        'to_contentobject_id',
1491
                        'relation_type',
1492
                    ]
1493
                )->from('ezcontentobject_link')
1494
                ->where('id = 1')
1495
        );
1496
    }
1497
1498
    /**
1499
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteRelation
1500
     */
1501
    public function testDeleteRelation()
1502
    {
1503
        $this->insertRelationFixture();
1504
1505
        self::assertEquals(4, $this->countContentRelations(57));
1506
1507
        $gateway = $this->getDatabaseGateway();
1508
        $gateway->deleteRelation(2, RelationValue::COMMON);
1509
1510
        self::assertEquals(3, $this->countContentRelations(57));
1511
    }
1512
1513
    /**
1514
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::deleteRelation
1515
     */
1516
    public function testDeleteRelationWithCompositeBitmask()
1517
    {
1518
        $this->insertRelationFixture();
1519
1520
        $gateway = $this->getDatabaseGateway();
1521
        $gateway->deleteRelation(11, RelationValue::COMMON);
1522
1523
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
1524
        $query = $this->getDatabaseHandler()->createSelectQuery();
1525
        $this->assertQueryResult(
1526
            [['relation_type' => RelationValue::LINK]],
1527
            $query->select(
1528
                ['relation_type']
1529
            )->from(
1530
                'ezcontentobject_link'
1531
            )->where(
1532
                $query->expr->eq('id', 11)
1533
            )
1534
        );
1535
    }
1536
1537
    /**
1538
     * Test for the updateAlwaysAvailableFlag() method.
1539
     *
1540
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateAlwaysAvailableFlag
1541
     */
1542 View Code Duplication
    public function testUpdateAlwaysAvailableFlagRemove()
1543
    {
1544
        $this->insertDatabaseFixture(
1545
            __DIR__ . '/../_fixtures/contentobjects.php'
1546
        );
1547
1548
        $gateway = $this->getDatabaseGateway();
1549
        $gateway->updateAlwaysAvailableFlag(103, false);
1550
1551
        $this->assertQueryResult(
1552
            [['id' => 2]],
1553
            $this->getDatabaseHandler()->createSelectQuery()->select(
1554
                ['language_mask']
1555
            )->from(
1556
                'ezcontentobject'
1557
            )->where(
1558
                'id = 103'
1559
            )
1560
        );
1561
1562
        $query = $this->getDatabaseHandler()->createSelectQuery();
1563
        $this->assertQueryResult(
1564
            [['language_id' => 2]],
1565
            $query->select(
1566
                ['language_id']
1567
            )->from(
1568
                'ezcontentobject_name'
1569
            )->where(
1570
                $query->expr->lAnd(
1571
                    $query->expr->eq('contentobject_id', 103),
1572
                    $query->expr->eq('content_version', 1)
1573
                )
1574
            )
1575
        );
1576
1577
        $query = $this->getDatabaseHandler()->createSelectQuery();
1578
        $this->assertQueryResult(
1579
            [
1580
                ['language_id' => 2],
1581
            ],
1582
            $query->selectDistinct(
1583
                ['language_id']
1584
            )->from(
1585
                'ezcontentobject_attribute'
1586
            )->where(
1587
                $query->expr->lAnd(
1588
                    $query->expr->eq('contentobject_id', 103),
1589
                    $query->expr->eq('version', 1)
1590
                )
1591
            )
1592
        );
1593
    }
1594
1595
    /**
1596
     * Test for the updateAlwaysAvailableFlag() method.
1597
     *
1598
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateAlwaysAvailableFlag
1599
     */
1600 View Code Duplication
    public function testUpdateAlwaysAvailableFlagAdd()
1601
    {
1602
        $this->insertDatabaseFixture(
1603
            __DIR__ . '/../_fixtures/contentobjects.php'
1604
        );
1605
1606
        $gateway = $this->getDatabaseGateway();
1607
        $gateway->updateAlwaysAvailableFlag(102, true);
1608
1609
        $this->assertQueryResult(
1610
            [['id' => 3]],
1611
            $this->getDatabaseHandler()->createSelectQuery()->select(
1612
                ['language_mask']
1613
            )->from(
1614
                'ezcontentobject'
1615
            )->where(
1616
                'id = 102'
1617
            )
1618
        );
1619
1620
        $query = $this->getDatabaseHandler()->createSelectQuery();
1621
        $this->assertQueryResult(
1622
            [['language_id' => 3]],
1623
            $query->select(
1624
                ['language_id']
1625
            )->from(
1626
                'ezcontentobject_name'
1627
            )->where(
1628
                $query->expr->lAnd(
1629
                    $query->expr->eq('contentobject_id', 102),
1630
                    $query->expr->eq('content_version', 1)
1631
                )
1632
            )
1633
        );
1634
1635
        $query = $this->getDatabaseHandler()->createSelectQuery();
1636
        $this->assertQueryResult(
1637
            [
1638
                ['language_id' => 3],
1639
            ],
1640
            $query->selectDistinct(
1641
                ['language_id']
1642
            )->from(
1643
                'ezcontentobject_attribute'
1644
            )->where(
1645
                $query->expr->lAnd(
1646
                    $query->expr->eq('contentobject_id', 102),
1647
                    $query->expr->eq('version', 1)
1648
                )
1649
            )
1650
        );
1651
    }
1652
1653
    /**
1654
     * Test for the updateAlwaysAvailableFlag() method.
1655
     *
1656
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateAlwaysAvailableFlag
1657
     */
1658 View Code Duplication
    public function testUpdateContentAddAlwaysAvailableFlagMultilingual()
1659
    {
1660
        $this->insertDatabaseFixture(
1661
            __DIR__ . '/../_fixtures/contentobjects_multilingual.php'
1662
        );
1663
1664
        $gateway = $this->getDatabaseGateway();
1665
        $contentMetadataUpdateStruct = new MetadataUpdateStruct(
1666
            [
1667
                'mainLanguageId' => 4,
1668
                'alwaysAvailable' => true,
1669
            ]
1670
        );
1671
        $gateway->updateContent(4, $contentMetadataUpdateStruct);
1672
1673
        $this->assertQueryResult(
1674
            [['id' => 7]],
1675
            $this->getDatabaseHandler()->createSelectQuery()->select(
1676
                ['language_mask']
1677
            )->from(
1678
                'ezcontentobject'
1679
            )->where(
1680
                'id = 4'
1681
            )
1682
        );
1683
1684
        $query = $this->getDatabaseHandler()->createSelectQuery();
1685
        $this->assertQueryResult(
1686
            [
1687
                ['id' => '7', 'language_id' => 2],
1688
                ['id' => '8', 'language_id' => 5],
1689
            ],
1690
            $query->selectDistinct(
1691
                ['id', 'language_id']
1692
            )->from(
1693
                'ezcontentobject_attribute'
1694
            )->where(
1695
                $query->expr->lAnd(
1696
                    $query->expr->eq('contentobject_id', 4),
1697
                    $query->expr->eq('version', 2)
1698
                )
1699
            )->orderBy('id')
1700
        );
1701
1702
        $query = $this->getDatabaseHandler()->createSelectQuery();
1703
        $this->assertQueryResult(
1704
            [
1705
                ['id' => '7', 'language_id' => 2],
1706
                ['id' => '8', 'language_id' => 5],
1707
            ],
1708
            $query->selectDistinct(
1709
                ['id', 'language_id']
1710
            )->from(
1711
                'ezcontentobject_attribute'
1712
            )->where(
1713
                $query->expr->lAnd(
1714
                    $query->expr->eq('contentobject_id', 4),
1715
                    $query->expr->eq('version', 1)
1716
                )
1717
            )->orderBy('id')
1718
        );
1719
    }
1720
1721
    /**
1722
     * Test for the updateAlwaysAvailableFlag() method.
1723
     *
1724
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::updateAlwaysAvailableFlag
1725
     */
1726 View Code Duplication
    public function testUpdateContentRemoveAlwaysAvailableFlagMultilingual()
1727
    {
1728
        $this->insertDatabaseFixture(
1729
            __DIR__ . '/../_fixtures/contentobjects_multilingual.php'
1730
        );
1731
1732
        $gateway = $this->getDatabaseGateway();
1733
        $contentMetadataUpdateStruct = new MetadataUpdateStruct(
1734
            [
1735
                'mainLanguageId' => 4,
1736
                'alwaysAvailable' => false,
1737
            ]
1738
        );
1739
        $gateway->updateContent(4, $contentMetadataUpdateStruct);
1740
1741
        $this->assertQueryResult(
1742
            [['id' => 6]],
1743
            $this->getDatabaseHandler()->createSelectQuery()->select(
1744
                ['language_mask']
1745
            )->from(
1746
                'ezcontentobject'
1747
            )->where(
1748
                'id = 4'
1749
            )
1750
        );
1751
1752
        $query = $this->getDatabaseHandler()->createSelectQuery();
1753
        $this->assertQueryResult(
1754
            [
1755
                ['id' => '7', 'language_id' => 2],
1756
                ['id' => '8', 'language_id' => 4],
1757
            ],
1758
            $query->selectDistinct(
1759
                ['id', 'language_id']
1760
            )->from(
1761
                'ezcontentobject_attribute'
1762
            )->where(
1763
                $query->expr->lAnd(
1764
                    $query->expr->eq('contentobject_id', 4),
1765
                    $query->expr->eq('version', 2)
1766
                )
1767
            )->orderBy('id')
1768
        );
1769
1770
        $query = $this->getDatabaseHandler()->createSelectQuery();
1771
        $this->assertQueryResult(
1772
            [
1773
                ['id' => '7', 'language_id' => 2],
1774
                ['id' => '8', 'language_id' => 5],
1775
            ],
1776
            $query->selectDistinct(
1777
                ['id', 'language_id']
1778
            )->from(
1779
                'ezcontentobject_attribute'
1780
            )->where(
1781
                $query->expr->lAnd(
1782
                    $query->expr->eq('contentobject_id', 4),
1783
                    $query->expr->eq('version', 1)
1784
                )
1785
            )->orderBy('id')
1786
        );
1787
    }
1788
1789 View Code Duplication
    public function testLoadVersionInfo()
1790
    {
1791
        $this->insertDatabaseFixture(
1792
            __DIR__ . '/../_fixtures/contentobjects.php'
1793
        );
1794
1795
        $gateway = $this->getDatabaseGateway();
1796
1797
        $resFirst = $gateway->loadVersionInfo(11, 1);
1798
        $resSecond = $gateway->loadVersionInfo(11, 2);
1799
1800
        $res = array_merge($resFirst, $resSecond);
1801
1802
        $orig = include __DIR__ . '/../_fixtures/extract_version_info_from_rows_multiple_versions.php';
1803
1804
        $this->assertEquals($orig, $res, 'Fixtures differ between what was previously stored(expected) and what it now generates(actual), this hints either some mistake in impl or that the fixture (../_fixtures/extract_content_from_rows_multiple_versions.php) and tests needs to be adapted.');
1805
    }
1806
1807
    /**
1808
     * Counts the number of relations in the database.
1809
     *
1810
     * @param int $fromId
1811
     * @param int $toId
1812
     *
1813
     * @return int
1814
     */
1815
    protected function countContentRelations($fromId = null, $toId = null)
1816
    {
1817
        $query = $this->getDatabaseHandler()->createSelectQuery();
1818
        $query->select('count(*)')
1819
            ->from('ezcontentobject_link');
1820
1821
        if ($fromId !== null) {
1822
            $query->where(
1823
                'from_contentobject_id=' . $fromId
1824
            );
1825
        }
1826
        if ($toId !== null) {
1827
            $query->where(
1828
                'to_contentobject_id=' . $toId
1829
            );
1830
        }
1831
1832
        $statement = $query->prepare();
1833
        $statement->execute();
1834
1835
        return (int)$statement->fetchColumn();
1836
    }
1837
1838
    /**
1839
     * Counts the number of fields.
1840
     *
1841
     * @param int $contentId
1842
     *
1843
     * @return int
1844
     */
1845 View Code Duplication
    protected function countContentFields($contentId = null)
1846
    {
1847
        $query = $this->getDatabaseHandler()->createSelectQuery();
1848
        $query->select('count(*)')
1849
            ->from('ezcontentobject_attribute');
1850
1851
        if ($contentId !== null) {
1852
            $query->where(
1853
                'contentobject_id=' . $contentId
1854
            );
1855
        }
1856
1857
        $statement = $query->prepare();
1858
        $statement->execute();
1859
1860
        return (int)$statement->fetchColumn();
1861
    }
1862
1863
    /**
1864
     * Counts the number of versions.
1865
     *
1866
     * @param int $contentId
1867
     *
1868
     * @return int
1869
     */
1870 View Code Duplication
    protected function countContentVersions($contentId = null)
1871
    {
1872
        $query = $this->getDatabaseHandler()->createSelectQuery();
1873
        $query->select('count(*)')
1874
            ->from('ezcontentobject_version');
1875
1876
        if ($contentId !== null) {
1877
            $query->where(
1878
                'contentobject_id=' . $contentId
1879
            );
1880
        }
1881
1882
        $statement = $query->prepare();
1883
        $statement->execute();
1884
1885
        return (int)$statement->fetchColumn();
1886
    }
1887
1888
    /**
1889
     * Counts the number of content names.
1890
     *
1891
     * @param int $contentId
1892
     *
1893
     * @return int
1894
     */
1895 View Code Duplication
    protected function countContentNames($contentId = null)
1896
    {
1897
        $query = $this->getDatabaseHandler()->createSelectQuery();
1898
        $query->select('count(*)')
1899
            ->from('ezcontentobject_name');
1900
1901
        if ($contentId !== null) {
1902
            $query->where(
1903
                'contentobject_id=' . $contentId
1904
            );
1905
        }
1906
1907
        $statement = $query->prepare();
1908
        $statement->execute();
1909
1910
        return (int)$statement->fetchColumn();
1911
    }
1912
1913
    /**
1914
     * Counts the number of content objects.
1915
     *
1916
     * @param int $contentId
1917
     *
1918
     * @return int
1919
     */
1920 View Code Duplication
    protected function countContent($contentId = null)
1921
    {
1922
        $query = $this->getDatabaseHandler()->createSelectQuery();
1923
        $query->select('count(*)')
1924
            ->from('ezcontentobject');
1925
1926
        if ($contentId !== null) {
1927
            $query->where(
1928
                'id=' . $contentId
1929
            );
1930
        }
1931
1932
        $statement = $query->prepare();
1933
        $statement->execute();
1934
1935
        return (int)$statement->fetchColumn();
1936
    }
1937
1938
    /**
1939
     * Stores $fixture in $file to be required as a fixture.
1940
     *
1941
     * @param string $file
1942
     * @param mixed $fixture
1943
     */
1944
    protected function storeFixture($file, $fixture)
1945
    {
1946
        file_put_contents(
1947
            $file,
1948
            "<?php\n\nreturn " . str_replace(" \n", "\n", var_export($fixture, true)) . ";\n"
1949
        );
1950
    }
1951
1952
    /**
1953
     * Returns a Field fixture.
1954
     *
1955
     * @return Field
1956
     */
1957
    protected function getFieldFixture()
1958
    {
1959
        $field = new Field();
1960
1961
        $field->fieldDefinitionId = 231;
1962
        $field->type = 'ezstring';
1963
        $field->languageCode = 'eng-GB';
1964
        $field->versionNo = 1;
1965
1966
        return $field;
1967
    }
1968
1969
    /**
1970
     * Returns a Field fixture in a different language.
1971
     *
1972
     * @return Field
1973
     */
1974
    protected function getOtherLanguageFieldFixture()
1975
    {
1976
        $field = $this->getFieldFixture();
1977
        $field->languageCode = 'eng-US';
1978
1979
        return $field;
1980
    }
1981
1982
    /**
1983
     * Returns a StorageFieldValue fixture.
1984
     *
1985
     * @return StorageFieldValue
1986
     */
1987
    protected function getStorageValueFixture()
1988
    {
1989
        $value = new StorageFieldValue();
1990
1991
        $value->dataFloat = 24.42;
1992
        $value->dataInt = 42;
1993
        $value->dataText = 'Test text';
1994
        $value->sortKeyInt = 23;
1995
        $value->sortKeyString = 'Test';
1996
1997
        return $value;
1998
    }
1999
2000
    /**
2001
     * Returns a ready to test DoctrineDatabase gateway.
2002
     *
2003
     * @throws \Doctrine\DBAL\DBALException
2004
     */
2005
    protected function getDatabaseGateway(): DoctrineDatabase
2006
    {
2007
        if (!isset($this->databaseGateway)) {
2008
            $connection = $this->getDatabaseConnection();
2009
            $this->databaseGateway = new DoctrineDatabase(
2010
                $connection,
2011
                $this->getSharedGateway(),
2012
                new DoctrineDatabase\QueryBuilder($connection),
2013
                $this->getLanguageHandler(),
2014
                $this->getLanguageMaskGenerator()
2015
            );
2016
        }
2017
2018
        return $this->databaseGateway;
2019
    }
2020
2021
    /**
2022
     * DoctrineDatabaseTest::getRelationCreateStructFixture().
2023
     *
2024
     * @return \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct
2025
     */
2026 View Code Duplication
    protected function getRelationCreateStructFixture()
2027
    {
2028
        $struct = new RelationCreateStruct();
2029
2030
        $struct->destinationContentId = 1;
2031
        $struct->sourceContentId = 1;
2032
        $struct->sourceContentVersionNo = 1;
2033
        $struct->sourceFieldDefinitionId = 0;
2034
        $struct->type = RelationValue::COMMON;
2035
2036
        return $struct;
2037
    }
2038
}
2039