Completed
Push — 6.7 ( 044712...96c013 )
by André
14:02
created

DoctrineDatabaseTest::testSetName()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\Type\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\Gateway;
10
11
use eZ\Publish\Core\Persistence\Legacy\Tests\Content\LanguageAwareTestCase;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase;
13
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
14
use eZ\Publish\SPI\Persistence\Content;
15
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
16
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
17
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
18
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
19
use eZ\Publish\SPI\Persistence\Content\Field;
20
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
21
use eZ\Publish\API\Repository\Values\Content\Relation as RelationValue;
22
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
23
24
/**
25
 * Test case for eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase.
26
 */
27
class DoctrineDatabaseTest extends LanguageAwareTestCase
28
{
29
    /**
30
     * Database gateway to test.
31
     *
32
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase
33
     */
34
    protected $databaseGateway;
35
36
    /**
37
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::__construct
38
     */
39
    public function testCtor()
40
    {
41
        $handlerMock = $this->getDatabaseHandler();
42
        $gateway = $this->getDatabaseGateway();
43
44
        $this->assertAttributeSame(
45
            $handlerMock,
46
            'dbHandler',
47
            $gateway
48
        );
49
    }
50
51
    /**
52
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::insertContentObject
53
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase::generateLanguageMask
54
     *
55
     * @todo Fix not available fields
56
     */
57
    public function testInsertContentObject()
58
    {
59
        $struct = $this->getCreateStructFixture();
60
61
        $gateway = $this->getDatabaseGateway();
62
        $gateway->insertContentObject($struct);
63
64
        $this->assertQueryResult(
65
            array(
66
                array(
67
                    'name' => 'Content name',
68
                    'contentclass_id' => '23',
69
                    'section_id' => '42',
70
                    'owner_id' => '13',
71
                    'current_version' => '1',
72
                    'initial_language_id' => '2',
73
                    'remote_id' => 'some_remote_id',
74
                    'language_mask' => '3',
75
                    'modified' => '0',
76
                    'published' => '0',
77
                    'status' => ContentInfo::STATUS_DRAFT,
78
                ),
79
            ),
80
            $this->getDatabaseHandler()
81
                ->createSelectQuery()
82
                ->select(
83
                    array(
84
                        'name',
85
                        'contentclass_id',
86
                        'section_id',
87
                        'owner_id',
88
                        'current_version',
89
                        'initial_language_id',
90
                        'remote_id',
91
                        'language_mask',
92
                        'modified',
93
                        'published',
94
                        'status',
95
                    )
96
                )->from('ezcontentobject')
97
        );
98
    }
99
100
    /**
101
     * Returns a Content fixture.
102
     *
103
     * @return \eZ\Publish\SPI\Persistence\Content\CreateStruct
104
     */
105
    protected function getCreateStructFixture()
106
    {
107
        $struct = new CreateStruct();
108
109
        $struct->typeId = 23;
110
        $struct->sectionId = 42;
111
        $struct->ownerId = 13;
112
        $struct->initialLanguageId = 2;
113
        $struct->remoteId = 'some_remote_id';
114
        $struct->alwaysAvailable = true;
115
        $struct->modified = 456;
116
        $struct->name = array(
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...
117
            'eng-US' => 'Content name',
118
        );
119
        $struct->fields = array(
120
            new Field(array('languageCode' => 'eng-US')),
121
        );
122
        $struct->locations = array();
123
124
        return $struct;
125
    }
126
127
    /**
128
     * Returns a Content fixture.
129
     *
130
     * @return \eZ\Publish\SPI\Persistence\Content
131
     */
132
    protected function getContentFixture()
133
    {
134
        $content = new Content();
135
136
        $content->versionInfo = new VersionInfo();
137
        $content->versionInfo->names = array(
138
            'eng-US' => 'Content name',
139
        );
140
        $content->versionInfo->status = VersionInfo::STATUS_PENDING;
141
142
        $content->versionInfo->contentInfo = new ContentInfo();
143
        $content->versionInfo->contentInfo->contentTypeId = 23;
144
        $content->versionInfo->contentInfo->sectionId = 42;
145
        $content->versionInfo->contentInfo->ownerId = 13;
146
        $content->versionInfo->contentInfo->currentVersionNo = 2;
147
        $content->versionInfo->contentInfo->mainLanguageCode = 'eng-US';
148
        $content->versionInfo->contentInfo->remoteId = 'some_remote_id';
149
        $content->versionInfo->contentInfo->alwaysAvailable = true;
150
        $content->versionInfo->contentInfo->publicationDate = 123;
151
        $content->versionInfo->contentInfo->modificationDate = 456;
152
        $content->versionInfo->contentInfo->isPublished = false;
153
        $content->versionInfo->contentInfo->name = 'Content name';
154
155
        return $content;
156
    }
157
158
    /**
159
     * Returns a Version fixture.
160
     *
161
     * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo
162
     */
163
    protected function getVersionFixture()
164
    {
165
        $version = new VersionInfo();
166
167
        $version->id = null;
168
        $version->versionNo = 1;
169
        $version->creatorId = 13;
170
        $version->status = 0;
171
        $version->creationDate = 1312278322;
172
        $version->modificationDate = 1312278323;
173
        $version->initialLanguageCode = 'eng-GB';
0 ignored issues
show
Documentation Bug introduced by
The property $initialLanguageCode was declared of type integer, but 'eng-GB' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

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