1 | <?php |
||
20 | class SectionTest extends Unit |
||
21 | { |
||
22 | /** |
||
23 | * @var Section |
||
24 | */ |
||
25 | private $converter; |
||
26 | |||
27 | /** |
||
28 | * Set the converter. |
||
29 | * |
||
30 | * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
||
31 | * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
||
32 | */ |
||
33 | protected function _before() |
||
34 | { |
||
35 | $this->converter = new Section(); |
||
36 | } |
||
37 | |||
38 | //============================================================================================================== |
||
39 | //================================================= TESTS ==================================================== |
||
40 | //============================================================================================================== |
||
41 | |||
42 | /** |
||
43 | * @dataProvider provideSections |
||
44 | * |
||
45 | * @param SectionModel $section |
||
46 | * @param array $definition |
||
47 | */ |
||
48 | public function testGetRecordDefinition(SectionModel $section, array $definition) |
||
49 | { |
||
50 | Craft::$app->controller->module->modelMapper->expects($this->exactly(1)) |
||
51 | ->method('export') |
||
52 | ->with($section->getEntryTypes()) |
||
53 | ->willReturn($definition['entryTypes']); |
||
54 | |||
55 | $result = $this->converter->getRecordDefinition($section); |
||
56 | |||
57 | $this->assertSame($definition, $result); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @dataProvider provideSections |
||
62 | * |
||
63 | * @param SectionModel $section |
||
64 | * @param array $definition |
||
65 | * @param bool $valid |
||
66 | */ |
||
67 | public function testSaveRecord(SectionModel $section, array $definition, bool $valid) |
||
68 | { |
||
69 | Craft::$app->sections->expects($this->exactly(1)) |
||
70 | ->method('saveSection') |
||
71 | ->with($section) |
||
72 | ->willReturn($valid); |
||
73 | |||
74 | Craft::$app->controller->module->modelMapper->expects($this->exactly($valid ? 1 : 0)) |
||
75 | ->method('import') |
||
76 | ->with($definition['entryTypes'], $section->getEntryTypes(), [ |
||
77 | 'sectionId' => $section->id, |
||
78 | ]) |
||
79 | ->willReturn($section->getEntryTypes()); |
||
80 | |||
81 | $result = $this->converter->saveRecord($section, $definition); |
||
82 | |||
83 | $this->assertSame($valid, $result); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @dataProvider provideSections |
||
88 | * |
||
89 | * @param SectionModel $section |
||
90 | */ |
||
91 | public function testDeleteRecord(SectionModel $section) |
||
92 | { |
||
93 | Craft::$app->sections->expects($this->exactly(1)) |
||
94 | ->method('deleteSection') |
||
95 | ->with($section); |
||
96 | |||
97 | $this->converter->deleteRecord($section); |
||
98 | } |
||
99 | |||
100 | //============================================================================================================== |
||
101 | //============================================== PROVIDERS =================================================== |
||
102 | //============================================================================================================== |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | public function provideSections() |
||
108 | { |
||
109 | $mockSection1 = $this->getMockSection(1); |
||
110 | $mockSection2 = $this->getMockSection(2); |
||
111 | |||
112 | return [ |
||
113 | 'valid section' => [ |
||
114 | 'section' => $mockSection1, |
||
115 | 'definition' => $this->getMockSectionDefinition($mockSection1), |
||
116 | 'validSave' => true, |
||
117 | ], |
||
118 | 'invalid section' => [ |
||
119 | 'section' => $mockSection2, |
||
120 | 'definition' => $this->getMockSectionDefinition($mockSection2), |
||
121 | 'validSave' => false, |
||
122 | ], |
||
123 | ]; |
||
124 | } |
||
125 | |||
126 | //============================================================================================================== |
||
127 | //================================================ HELPERS =================================================== |
||
128 | //============================================================================================================== |
||
129 | |||
130 | /** |
||
131 | * @param SectionModel $mockSection |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | private function getMockSectionDefinition(SectionModel $mockSection) |
||
136 | { |
||
137 | $siteSettingsDef = []; |
||
138 | foreach ($mockSection->getSiteSettings() as $siteSetting) { |
||
139 | $siteSettingsDef[$siteSetting->site->handle] = [ |
||
140 | 'class' => get_class($siteSetting), |
||
141 | 'attributes' => [ |
||
142 | 'enabledByDefault' => true, |
||
143 | 'hasUrls' => null, |
||
144 | 'uriFormat' => null, |
||
145 | 'template' => null, |
||
146 | ], |
||
147 | ]; |
||
148 | } |
||
149 | |||
150 | return [ |
||
151 | 'class' => get_class($mockSection), |
||
152 | 'attributes' => [ |
||
153 | 'name' => 'sectionName'.$mockSection->id, |
||
154 | 'handle' => 'sectionHandle'.$mockSection->id, |
||
155 | 'type' => null, |
||
156 | 'maxLevels' => null, |
||
157 | 'enableVersioning' => true, |
||
158 | 'propagateEntries' => true, |
||
159 | ], |
||
160 | 'siteSettings' => $siteSettingsDef, |
||
161 | 'entryTypes' => [ |
||
162 | 'entryTypeDefinition1', |
||
163 | 'entryTypeDefinition2', |
||
164 | ], |
||
165 | ]; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param int $sectionId |
||
170 | * |
||
171 | * @return Mock|SectionModel |
||
172 | */ |
||
173 | private function getMockSection(int $sectionId) |
||
174 | { |
||
175 | $mockSection = $this->getMockBuilder(SectionModel::class) |
||
176 | ->setMethods(['getGroup', 'getEntryTypes', 'getSiteSettings']) |
||
177 | ->disableOriginalConstructor() |
||
178 | ->getMock(); |
||
179 | |||
180 | $mockSection->id = $sectionId; |
||
181 | $mockSection->handle = 'sectionHandle'.$sectionId; |
||
182 | $mockSection->name = 'sectionName'.$sectionId; |
||
183 | |||
184 | $mockSection->expects($this->any()) |
||
185 | ->method('getEntryTypes') |
||
186 | ->willReturn([ |
||
187 | $this->getMockEntryType(1), |
||
188 | $this->getMockEntryType(2), |
||
189 | ]); |
||
190 | |||
191 | $mockSection->expects($this->any()) |
||
192 | ->method('getSiteSettings') |
||
193 | ->willReturn([$this->getMockSiteSettings()]); |
||
194 | |||
195 | return $mockSection; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get a mock entry block type. |
||
200 | * |
||
201 | * @param int $blockId |
||
202 | * |
||
203 | * @return Mock|EntryType |
||
204 | */ |
||
205 | private function getMockEntryType($blockId) |
||
206 | { |
||
207 | $mockBlockType = $this->getMockBuilder(EntryType::class) |
||
208 | ->disableOriginalConstructor() |
||
209 | ->getmock(); |
||
210 | |||
211 | $mockBlockType->id = $blockId; |
||
212 | $mockBlockType->handle = 'blockHandle'.$blockId; |
||
213 | |||
214 | return $mockBlockType; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Get mock siteSettings. |
||
219 | * |
||
220 | * @param string $class |
||
221 | * |
||
222 | * @return Mock|Section_SiteSettings |
||
223 | */ |
||
224 | private function getMockSiteSettings() |
||
225 | { |
||
226 | $mockSiteSettings = $this->getMockBuilder(Section_SiteSettings::class) |
||
227 | ->setMethods(['getSite']) |
||
228 | ->getMock(); |
||
229 | |||
230 | $mockSiteSettings->expects($this->any()) |
||
231 | ->method('getSite') |
||
232 | ->willReturn($this->getMockSite()); |
||
233 | |||
234 | return $mockSiteSettings; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Get a mock site. |
||
239 | * |
||
240 | * @return Mock|Site |
||
241 | */ |
||
242 | private function getMockSite() |
||
243 | { |
||
244 | $mockSite = $this->getMockBuilder(Site::class)->getMock(); |
||
245 | $mockSite->handle = 'default'; |
||
246 | |||
247 | return $mockSite; |
||
248 | } |
||
249 | } |
||
250 |