/
JComponentRouterViewTest.php
353 lines (317 loc) · 8.63 KB
/
JComponentRouterViewTest.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* @package Joomla.UnitTest
* @subpackage Component
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
require_once __DIR__ . '/stubs/JComponentRouterViewInspector.php';
require_once __DIR__ . '/stubs/componentrouterrule.php';
require_once __DIR__ . '/stubs/JCategoriesMock.php';
/**
* Test class for JComponentRouterAdvanced.
*
* @package Joomla.UnitTest
* @subpackage Component
* @since 3.5
*/
class JComponentRouterViewTest extends TestCaseDatabase
{
/**
* Object under test
*
* @var JComponentRouterView
* @since 3.5
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @return void
*
* @since 3.5
*/
protected function setUp()
{
parent::setUp();
$app = $this->getMockCmsApp();
$this->object = new JComponentRouterViewInspector($app, $app->getMenu());
}
/**
* Gets the data set to be loaded into the database during setup
*
* @return PHPUnit_Extensions_Database_DataSet_CsvDataSet
*
* @since 3.2
*/
protected function getDataSet()
{
$dataSet = new PHPUnit_Extensions_Database_DataSet_CsvDataSet(',', "'", '\\');
$dataSet->addTable('jos_categories', JPATH_TEST_DATABASE . '/jos_categories.csv');
return $dataSet;
}
/**
* Tests the registerView() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::registerView
*/
public function testRegisterView()
{
$views = $this->getComContentViews();
foreach ($views as $view)
{
$this->object->registerView($view);
}
$this->assertEquals($views, $this->object->get('views'));
}
/**
* Tests the getViews() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::getViews
*/
public function testGetViews()
{
$views = $this->getComContentViews();
foreach ($views as $view)
{
$this->object->registerView($view);
}
$this->assertEquals($views, $this->object->getViews());
}
/**
* Cases for testGetPath
*
* @return array
*
* @since 3.5
*/
public function casesGetPath()
{
$cases = array();
// No view, so we don't have a path to return.
$cases[] = array(array('task' => 'edit'), array());
// View without any parents and children
$cases[] = array(array('view' => 'form'), array('form' => true));
// View without any parents, but with children
$cases[] = array(array('view' => 'categories'), array('categories' => array()));
// View with parent and children
$cases[] = array(array('view' => 'category', 'id' => '9'), array('category' => array(9 => '9:uncategorised'), 'categories' => array(9 => '9:uncategorised')));
//View with parent, no children
$cases[] = array(array('view' => 'article', 'id' => '42:question-for-everything', 'catid' => '9'),
array(
'article' => array(42 => '42:question-for-everything'),
'category' => array(9 => '9:uncategorised'),
'categories' => array(9 => '9:uncategorised')
)
);
//View with parent, no children and nested view
$cases[] = array(array('view' => 'article', 'id' => '42:question-for-everything', 'catid' => '20'),
array(
'article' => array(42 => '42:question-for-everything'),
'category' => array(20 => '20:extensions',
19 => '19:joomla',
14 => '14:sample-data-articles'
),
'categories' => array(20 => '20:extensions',
19 => '19:joomla',
14 => '14:sample-data-articles'
)
)
);
return $cases;
}
/**
* Tests the getPath() method
*
* @return void
*
* @dataProvider casesGetPath
* @since 3.5
* @covers JComponentRouterView::getPath
*/
public function testGetPath($input, $result)
{
// This test requires an application registered to JFactory
$this->saveFactoryState();
JFactory::$application = $this->object->app;
$views = $this->getComContentViews();
$this->object->set('name', 'unittest');
foreach ($views as $view)
{
$this->object->registerView($view);
}
$this->assertEquals($result, $this->object->getPath($input));
$this->restoreFactoryState();
}
/**
* Tests the getRules() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::getRules
*/
public function testGetRules()
{
$rule = new TestComponentRouterRule($this->object);
$this->object->attachRule($rule);
$this->assertEquals(array($rule), $this->object->getRules());
}
/**
* Tests the attachRules() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::attachRules
*/
public function testAttachRules()
{
$rule = new TestComponentRouterRule($this->object);
$this->assertEquals(array(), $this->object->getRules());
$this->object->attachRules(array($rule));
$this->assertEquals(array($rule), $this->object->getRules());
}
/**
* Tests the attachRule() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::attachRule
*/
public function testAttachRule()
{
$rule = new TestComponentRouterRule($this->object);
$this->assertEquals(array(), $this->object->get('rules'));
$this->object->attachRule($rule);
$this->assertEquals(array($rule), $this->object->get('rules'));
$this->object->attachRule($rule);
$this->assertEquals(array($rule, $rule), $this->object->get('rules'));
}
/**
* Tests the detachRule() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::detachRule
*/
public function testDetachRule()
{
$rule = new TestComponentRouterRule($this->object);
$this->object->attachRule($rule);
$this->assertEquals(array($rule), $this->object->get('rules'));
$this->assertTrue($this->object->detachRule($rule));
$this->assertEquals(array(), $this->object->get('rules'));
$this->assertFalse($this->object->detachRule($rule));
}
/**
* Tests the preprocess() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::preprocess
*/
public function testPreprocess()
{
$rule = new TestComponentRouterRule($this->object);
$this->object->attachRule($rule);
$this->assertEquals(array('key' => 'value', 'testrule' => 'yes'), $this->object->preprocess(array('key' => 'value')));
}
/**
* Tests the build() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::build
*/
public function testBuild()
{
$rule = new TestComponentRouterRule($this->object);
$this->object->attachRule($rule);
$query = array('key' => 'value', 'test' => 'true');
$this->assertEquals(array('testrule-run'), $this->object->build($query));
$this->assertEquals(array('key' => 'value'), $query);
}
/**
* Tests the parse() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::parse
*/
public function testParse()
{
$rule = new TestComponentRouterRule($this->object);
$this->object->attachRule($rule);
$segments = array('testrun', 'getsdropped');
$this->assertEquals(array('testparse' => 'run'), $this->object->parse($segments));
$this->assertEquals(array('testrun'), $segments);
}
/**
* Tests the getName() method
*
* @return void
*
* @since 3.5
* @covers JComponentRouterView::getName
*/
public function testGetName()
{
$this->object->set('name', 'test');
$this->assertEquals('test', $this->object->getName());
$this->object->set('name', null);
$this->assertEquals('jcomponent', $this->object->getName());
}
/**
* Tests the getName() method and if it throws the right Exception
*
* @return void
*
* @since 3.5
* @expectedException Exception
* @covers JComponentRouterView::getName
*/
public function testGetNameException()
{
$object = new FakeComponentURLCreator($this->object->app, $this->object->menu);
$object->getName();
}
/**
* As view testdata, use the view configuration of com_content
*
* @return array|JComponentRouterViewconfiguration
*/
protected function getComContentViews()
{
$categories = new JComponentRouterViewconfiguration('categories');
$categories->setKey('id');
$category = new JComponentRouterViewconfiguration('category');
$category->setKey('id')->setParent($categories, 'catid')->setNestable()->addLayout('blog');
$article = new JComponentRouterViewconfiguration('article');
$article->setKey('id')->setParent($category, 'catid');
$archive = new JComponentRouterViewconfiguration('archive');
$featured = new JComponentRouterViewconfiguration('featured');
$form = new JComponentRouterViewconfiguration('form');
return array(
'categories' => $categories,
'category' => $category,
'article' => $article,
'archive' => $archive,
'featured' => $featured,
'form' => $form
);
}
}