|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the AuthorTest 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\FieldValue\Converter; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\FieldType\Author\Type as AuthorType; |
|
12
|
|
|
use eZ\Publish\Core\FieldType\FieldSettings; |
|
13
|
|
|
use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints; |
|
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\FieldValue; |
|
15
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; |
|
16
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition; |
|
17
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter; |
|
18
|
|
|
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition as SPIFieldDefinition; |
|
19
|
|
|
use PHPUnit\Framework\TestCase; |
|
20
|
|
|
use DOMDocument; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Test case for Author converter in Legacy storage. |
|
24
|
|
|
* |
|
25
|
|
|
* @group fieldType |
|
26
|
|
|
* @group ezauthor |
|
27
|
|
|
*/ |
|
28
|
|
|
class AuthorTest extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $converter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \eZ\Publish\Core\FieldType\Author\Author[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private $authors; |
|
39
|
|
|
|
|
40
|
|
|
protected function setUp() |
|
41
|
|
|
{ |
|
42
|
|
|
parent::setUp(); |
|
43
|
|
|
$this->converter = new AuthorConverter(); |
|
44
|
|
|
$this->authors = array( |
|
|
|
|
|
|
45
|
|
|
array('id' => 21, 'name' => 'Boba Fett', 'email' => '[email protected]'), |
|
46
|
|
|
array('id' => 42, 'name' => 'Darth Vader', 'email' => '[email protected]'), |
|
47
|
|
|
array('id' => 63, 'name' => 'Luke Skywalker', 'email' => '[email protected]'), |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function tearDown() |
|
52
|
|
|
{ |
|
53
|
|
|
unset($this->authors); |
|
54
|
|
|
parent::tearDown(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toStorageValue |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testToStorageValue() |
|
61
|
|
|
{ |
|
62
|
|
|
$value = new FieldValue(); |
|
63
|
|
|
$value->data = $this->authors; |
|
64
|
|
|
$storageFieldValue = new StorageFieldValue(); |
|
65
|
|
|
|
|
66
|
|
|
$this->converter->toStorageValue($value, $storageFieldValue); |
|
67
|
|
|
$doc = new DOMDocument('1.0', 'utf-8'); |
|
68
|
|
|
self::assertTrue($doc->loadXML($storageFieldValue->dataText)); |
|
69
|
|
|
|
|
70
|
|
|
$authorsXml = $doc->getElementsByTagName('author'); |
|
71
|
|
|
self::assertSame(count($this->authors), $authorsXml->length); |
|
72
|
|
|
|
|
73
|
|
|
// Loop against XML nodes and compare them to the real Author objects. |
|
74
|
|
|
// Then remove Author from $this->authors |
|
75
|
|
|
// This way, we can check if all authors have been converted in XML |
|
76
|
|
View Code Duplication |
foreach ($authorsXml as $authorXml) { |
|
|
|
|
|
|
77
|
|
|
foreach ($this->authors as $i => $author) { |
|
78
|
|
|
if ($authorXml->getAttribute('id') == $author['id']) { |
|
79
|
|
|
self::assertSame($author['name'], $authorXml->getAttribute('name')); |
|
80
|
|
|
self::assertSame($author['email'], $authorXml->getAttribute('email')); |
|
81
|
|
|
unset($this->authors[$i]); |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
self::assertEmpty($this->authors, 'All authors have not been converted as expected'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toFieldValue |
|
92
|
|
|
*/ |
|
93
|
|
|
public function testToFieldValue() |
|
94
|
|
|
{ |
|
95
|
|
|
$storageFieldValue = new StorageFieldValue(); |
|
96
|
|
|
$storageFieldValue->dataText = <<<EOT |
|
97
|
|
|
<?xml version="1.0" encoding="utf-8"?> |
|
98
|
|
|
<ezauthor> |
|
99
|
|
|
<authors> |
|
100
|
|
|
<author id="1" name="Boba Fett" email="[email protected]"/> |
|
101
|
|
|
<author id="2" name="Darth Vader" email="[email protected]"/> |
|
102
|
|
|
<author id="3" name="Luke Skywalker" email="[email protected]"/> |
|
103
|
|
|
</authors> |
|
104
|
|
|
</ezauthor> |
|
105
|
|
|
EOT; |
|
106
|
|
|
$doc = new DOMDocument('1.0', 'utf-8'); |
|
107
|
|
|
self::assertTrue($doc->loadXML($storageFieldValue->dataText)); |
|
108
|
|
|
$authorsXml = $doc->getElementsByTagName('author'); |
|
|
|
|
|
|
109
|
|
|
$fieldValue = new FieldValue(); |
|
110
|
|
|
|
|
111
|
|
|
$this->converter->toFieldValue($storageFieldValue, $fieldValue); |
|
112
|
|
|
self::assertInternalType('array', $fieldValue->data); |
|
113
|
|
|
|
|
114
|
|
|
$authorsXml = $doc->getElementsByTagName('author'); |
|
115
|
|
|
self::assertSame($authorsXml->length, count($fieldValue->data)); |
|
116
|
|
|
|
|
117
|
|
|
$aAuthors = $fieldValue->data; |
|
118
|
|
View Code Duplication |
foreach ($fieldValue->data as $i => $author) { |
|
|
|
|
|
|
119
|
|
|
foreach ($authorsXml as $authorXml) { |
|
120
|
|
|
if ($authorXml->getAttribute('id') == $author['id']) { |
|
121
|
|
|
self::assertSame($authorXml->getAttribute('name'), $author['name']); |
|
122
|
|
|
self::assertSame($authorXml->getAttribute('email'), $author['email']); |
|
123
|
|
|
unset($aAuthors[$i]); |
|
124
|
|
|
break; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
self::assertEmpty($aAuthors, 'All authors have not been converted as expected from storage'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toStorageFieldDefinition |
|
133
|
|
|
*/ |
|
134
|
|
View Code Duplication |
public function testToStorageFieldDefinitionDefaultCurrentUser() |
|
135
|
|
|
{ |
|
136
|
|
|
$storageFieldDef = new StorageFieldDefinition(); |
|
137
|
|
|
$fieldTypeConstraints = new FieldTypeConstraints(); |
|
138
|
|
|
$fieldTypeConstraints->fieldSettings = new FieldSettings( |
|
139
|
|
|
array( |
|
140
|
|
|
'defaultAuthor' => AuthorType::DEFAULT_CURRENT_USER, |
|
141
|
|
|
) |
|
142
|
|
|
); |
|
143
|
|
|
$fieldDef = new SPIFieldDefinition( |
|
144
|
|
|
array( |
|
145
|
|
|
'fieldTypeConstraints' => $fieldTypeConstraints, |
|
146
|
|
|
) |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$this->converter->toStorageFieldDefinition($fieldDef, $storageFieldDef); |
|
150
|
|
|
self::assertSame( |
|
151
|
|
|
AuthorType::DEFAULT_CURRENT_USER, |
|
152
|
|
|
$storageFieldDef->dataInt1 |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter::toStorageFieldDefinition |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function testToStorageFieldDefinitionDefaultEmpty() |
|
160
|
|
|
{ |
|
161
|
|
|
$storageFieldDef = new StorageFieldDefinition(); |
|
162
|
|
|
$fieldTypeConstraints = new FieldTypeConstraints(); |
|
163
|
|
|
$fieldTypeConstraints->fieldSettings = new FieldSettings( |
|
164
|
|
|
array( |
|
165
|
|
|
'defaultAuthor' => AuthorType::DEFAULT_VALUE_EMPTY, |
|
166
|
|
|
) |
|
167
|
|
|
); |
|
168
|
|
|
$fieldDef = new SPIFieldDefinition( |
|
169
|
|
|
array( |
|
170
|
|
|
'fieldTypeConstraints' => $fieldTypeConstraints, |
|
171
|
|
|
) |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
$this->converter->toStorageFieldDefinition($fieldDef, $storageFieldDef); |
|
175
|
|
|
self::assertSame( |
|
176
|
|
|
AuthorType::DEFAULT_VALUE_EMPTY, |
|
177
|
|
|
$storageFieldDef->dataInt1 |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
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..