1 | <?php |
||
21 | class EntryTypeTest extends Unit |
||
22 | { |
||
23 | /** |
||
24 | * @var EntryType |
||
25 | */ |
||
26 | private $converter; |
||
27 | |||
28 | /** |
||
29 | * Set the converter. |
||
30 | * |
||
31 | * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
||
32 | * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
||
33 | */ |
||
34 | protected function _before() |
||
35 | { |
||
36 | $this->converter = new EntryType(); |
||
37 | } |
||
38 | |||
39 | //============================================================================================================== |
||
40 | //================================================= TESTS ==================================================== |
||
41 | //============================================================================================================== |
||
42 | |||
43 | /** |
||
44 | * @dataProvider provideEntryTypes |
||
45 | * |
||
46 | * @param EntryTypeModel $entryType |
||
47 | * @param array $definition |
||
48 | */ |
||
49 | public function testGetRecordDefinition(EntryTypeModel $entryType, array $definition) |
||
50 | { |
||
51 | $result = $this->converter->getRecordDefinition($entryType); |
||
52 | |||
53 | $this->assertSame($definition, $result); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @dataProvider provideEntryTypes |
||
58 | * |
||
59 | * @param EntryTypeModel $entryType |
||
60 | * @param array $definition |
||
61 | * @param array $defaultAttributes |
||
62 | */ |
||
63 | public function testSetRecordAttributes(EntryTypeModel $entryType, array $definition, array $defaultAttributes) |
||
64 | { |
||
65 | $newEntryType = $this->getMockBuilder(EntryTypeModel::class) |
||
66 | ->setMethods(['setFieldLayout']) |
||
67 | ->getMock(); |
||
68 | |||
69 | $newEntryType->expects($this->exactly(1)) |
||
70 | ->method('setFieldLayout'); |
||
71 | |||
72 | Craft::$app->fields->expects($this->any()) |
||
73 | ->method('getFieldByHandle') |
||
74 | ->willReturn($this->getMockField($entryType->id)); |
||
75 | |||
76 | $this->converter->setRecordAttributes($newEntryType, $definition, $defaultAttributes); |
||
77 | |||
78 | $this->assertSame($defaultAttributes['sectionId'], $newEntryType->sectionId); |
||
79 | $this->assertSame($entryType->name, $newEntryType->name); |
||
80 | $this->assertSame($entryType->handle, $newEntryType->handle); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @dataProvider provideEntryTypes |
||
85 | * |
||
86 | * @param EntryTypeModel $entryType |
||
87 | * @param array $definition |
||
88 | */ |
||
89 | public function testSaveRecord(EntryTypeModel $entryType, array $definition) |
||
90 | { |
||
91 | Craft::$app->sections->expects($this->exactly(1)) |
||
92 | ->method('saveEntryType') |
||
93 | ->with($entryType) |
||
94 | ->willReturn(true); |
||
95 | |||
96 | $result = $this->converter->saveRecord($entryType, $definition); |
||
97 | |||
98 | $this->assertTrue($result); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @dataProvider provideEntryTypes |
||
103 | * |
||
104 | * @param EntryTypeModel $entryType |
||
105 | */ |
||
106 | public function testDeleteRecord(EntryTypeModel $entryType) |
||
107 | { |
||
108 | Craft::$app->sections->expects($this->exactly(1)) |
||
109 | ->method('deleteEntryType') |
||
110 | ->with($entryType); |
||
111 | |||
112 | $this->converter->deleteRecord($entryType); |
||
113 | } |
||
114 | |||
115 | //============================================================================================================== |
||
116 | //============================================== PROVIDERS =================================================== |
||
117 | //============================================================================================================== |
||
118 | |||
119 | /** |
||
120 | * @return array |
||
121 | */ |
||
122 | public function provideEntryTypes() |
||
123 | { |
||
124 | $mockEntryType = $this->getMockEntryType(1); |
||
125 | |||
126 | return [ |
||
127 | 'valid entryType' => [ |
||
128 | 'entryType' => $mockEntryType, |
||
129 | 'definition' => $this->getMockEntryTypeDefinition($mockEntryType), |
||
130 | 'defaultAttributes' => ['sectionId' => 1], |
||
131 | ], |
||
132 | ]; |
||
133 | } |
||
134 | |||
135 | //============================================================================================================== |
||
136 | //================================================ HELPERS =================================================== |
||
137 | //============================================================================================================== |
||
138 | |||
139 | /** |
||
140 | * @param EntryTypeModel $mockEntryType |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | private function getMockEntryTypeDefinition(EntryTypeModel $mockEntryType) |
||
145 | { |
||
146 | return [ |
||
147 | 'class' => get_class($mockEntryType), |
||
148 | 'attributes' => [ |
||
149 | 'name' => 'entryTypeName'.$mockEntryType->id, |
||
150 | 'handle' => 'entryTypeHandle'.$mockEntryType->id, |
||
151 | 'hasTitleField' => true, |
||
152 | 'titleLabel' => 'Title', |
||
153 | 'titleFormat' => null, |
||
154 | ], |
||
155 | 'fieldLayout' => $this->getMockFieldLayoutDefinition($mockEntryType->getFieldLayout()), |
||
156 | ]; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get mock field layout definition. |
||
161 | * |
||
162 | * @param FieldLayout $fieldLayout |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | private function getMockFieldLayoutDefinition(FieldLayout $fieldLayout) |
||
167 | { |
||
168 | $tabsDef = []; |
||
169 | foreach ($fieldLayout->getTabs() as $tab) { |
||
170 | $tabsDef[$tab->name] = []; |
||
171 | foreach ($tab->getFields() as $field) { |
||
172 | $tabsDef[$tab->name][$field->handle] = $field->required; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return [ |
||
177 | 'tabs' => $tabsDef, |
||
178 | ]; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param int $entryTypeId |
||
183 | * |
||
184 | * @return Mock|EntryTypeModel |
||
185 | */ |
||
186 | private function getMockEntryType(int $entryTypeId) |
||
187 | { |
||
188 | $mockEntryType = $this->getMockBuilder(EntryTypeModel::class) |
||
189 | ->setMethods(['getFieldLayout']) |
||
190 | ->disableOriginalConstructor() |
||
191 | ->getMock(); |
||
192 | |||
193 | $mockEntryType->id = $entryTypeId; |
||
194 | $mockEntryType->fieldLayoutId = $entryTypeId; |
||
195 | $mockEntryType->handle = 'entryTypeHandle'.$entryTypeId; |
||
196 | $mockEntryType->name = 'entryTypeName'.$entryTypeId; |
||
197 | |||
198 | $mockField = $this->getMockField($entryTypeId); |
||
199 | |||
200 | $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock(); |
||
201 | $mockFieldLayoutTab = $this->getMockBuilder(FieldLayoutTab::class)->getMock(); |
||
202 | $mockFieldLayoutTab->name = 'Content'; |
||
203 | |||
204 | $mockFieldLayout->expects($this->any()) |
||
205 | ->method('getTabs') |
||
206 | ->willReturn([$mockFieldLayoutTab]); |
||
207 | |||
208 | $mockFieldLayoutTab->expects($this->any()) |
||
209 | ->method('getFields') |
||
210 | ->willReturn([$mockField]); |
||
211 | |||
212 | $mockEntryType->expects($this->any()) |
||
213 | ->method('getFieldLayout') |
||
214 | ->willReturn($mockFieldLayout); |
||
215 | |||
216 | return $mockEntryType; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get a mock field. |
||
221 | * |
||
222 | * @param int $fieldId |
||
223 | * |
||
224 | * @return Mock|FieldModel |
||
225 | */ |
||
226 | private function getMockField(int $fieldId) |
||
227 | { |
||
228 | $mockField = $this->getMockbuilder(FieldModel::class) |
||
229 | ->setMethods([]) |
||
230 | ->getMock(); |
||
231 | |||
232 | $mockField->id = $fieldId; |
||
233 | $mockField->handle = 'field'.$fieldId; |
||
234 | $mockField->required = true; |
||
235 | |||
236 | return $mockField; |
||
237 | } |
||
238 | } |
||
239 |