forked from ezsystems/ezpublish-kernel
/
RelationBaseIntegrationTest.php
110 lines (98 loc) · 3.49 KB
/
RelationBaseIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* File contains: eZ\Publish\API\Repository\Tests\FieldType\RelationBaseIntegrationTest class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\API\Repository\Tests\FieldType;
use eZ\Publish\Core\Repository\Values\Content\Relation;
use eZ\Publish\API\Repository\Values\Content\Content;
/**
* Base integration test for field types handling content relations.
*
* @group integration
* @group field-type
* @group relation
* @deprecated 6.1 use RelationSearchBaseIntegrationTestTrait instead.
*/
abstract class RelationBaseIntegrationTest extends BaseIntegrationTest
{
/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @return \eZ\Publish\API\Repository\Values\Content\Relation[]
*/
abstract public function getCreateExpectedRelations(Content $content);
/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @return \eZ\Publish\API\Repository\Values\Content\Relation[]
*/
abstract public function getUpdateExpectedRelations(Content $content);
/**
* Tests relation processing on field create.
*/
public function testCreateContentRelationsProcessedCorrect()
{
$content = $this->createContent($this->getValidCreationFieldData());
$this->assertEquals(
$this->normalizeRelations(
$this->getCreateExpectedRelations($content)
),
$this->normalizeRelations(
$this->getRepository()->getContentService()->loadRelations($content->versionInfo)
)
);
}
/**
* Tests relation processing on field update.
*/
public function testUpdateContentRelationsProcessedCorrect()
{
$content = $this->updateContent($this->getValidUpdateFieldData());
$this->assertEquals(
$this->normalizeRelations(
$this->getUpdateExpectedRelations($content)
),
$this->normalizeRelations(
$this->getRepository()->getContentService()->loadRelations($content->versionInfo)
)
);
}
/**
* Normalizes given $relations for easier comparison.
*
* @param \eZ\Publish\Core\Repository\Values\Content\Relation[] $relations
*
* @return \eZ\Publish\Core\Repository\Values\Content\Relation[]
*/
protected function normalizeRelations(array $relations)
{
usort(
$relations,
function (Relation $a, Relation $b) {
if ($a->type == $b->type) {
return $a->destinationContentInfo->id < $b->destinationContentInfo->id ? 1 : -1;
}
return $a->type < $b->type ? 1 : -1;
}
);
$normalized = array_map(
function (Relation $relation) {
$newRelation = new Relation(
array(
'id' => null,
'sourceFieldDefinitionIdentifier' => $relation->sourceFieldDefinitionIdentifier,
'type' => $relation->type,
'sourceContentInfo' => $relation->sourceContentInfo,
'destinationContentInfo' => $relation->destinationContentInfo,
)
);
return $newRelation;
},
$relations
);
return $normalized;
}
}