Completed
Push — master ( 9420d2...f7d146 )
by Alex
03:34
created

PageControllerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 334
Duplicated Lines 23.05 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 12
Bugs 5 Features 2
Metric Value
wmc 12
c 12
b 5
f 2
lcom 1
cbo 6
dl 77
loc 334
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testNoPageWithSlug() 0 6 1
A testNoHomepage() 0 8 1
B testOneHomepage() 38 38 1
B testOneHomepageWithLocale() 39 39 1
A testTree() 0 48 1
B testMetas() 0 34 1
B testParentAndChildrenDontReverse() 0 29 1
A testAllTypesOfPagesForHomepage() 0 53 3
A testBreadcrumbsDesign() 0 60 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
* This file is part of the OrbitaleCmsBundle package.
5
*
6
* (c) Alexandre Rock Ancelet <[email protected]>
7
*
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
10
*/
11
12
namespace Orbitale\Bundle\CmsBundle\Tests\Controller;
13
14
use Doctrine\ORM\EntityManager;
15
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\AbstractTestCase;
16
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
17
18
class PageControllerTest extends AbstractTestCase
19
{
20
    public function testNoHomepage()
21
    {
22
        $error   = 'No homepage has been configured. Please check your existing pages or create a homepage in your application. (404 Not Found)';
23
        $client  = static::createClient();
24
        $crawler = $client->request('GET', '/page/');
25
        static::assertEquals($error, trim($crawler->filter('title')->html()));
26
        static::assertEquals(404, $client->getResponse()->getStatusCode());
27
    }
28
29
    public function testNoPageWithSlug()
30
    {
31
        $client = static::createClient();
32
        $client->request('GET', '/page/inexistent-slug');
33
        static::assertEquals(404, $client->getResponse()->getStatusCode());
34
    }
35
36 View Code Duplication
    public function testOneHomepage()
37
    {
38
        $client = static::createClient();
39
40
        $homepage = $this->createPage([
41
            'homepage' => true,
42
            'enabled'  => true,
43
            'slug'     => 'home',
44
            'title'    => 'My homepage',
45
            'host'     => 'localhost',
46
            'content'  => 'Hello world!',
47
        ]);
48
49
        /** @var EntityManager $em */
50
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
51
        $em->persist($homepage);
52
        $em->flush();
53
54
        $crawler = $client->request('GET', '/page/');
55
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
56
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
57
        static::assertContains($homepage->getContent(), trim($crawler->filter('article')->html()));
58
59
        // Repeat with the homepage directly in the url
60
61
        // First, check that any right trimming "/" will redirect
62
        $client->request('GET', '/page/home/');
63
        static::assertTrue($client->getResponse()->isRedirect('/page/home'));
64
        $client->followRedirect();
65
66
        // Check that the homepage with slug is redirected to the root page
67
        static::assertTrue($client->getResponse()->isRedirect('/page'));
68
        $crawler = $client->followRedirect();
69
70
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
71
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
72
        static::assertContains($homepage->getContent(), trim($crawler->filter('article')->html()));
73
    }
74
75 View Code Duplication
    public function testOneHomepageWithLocale()
76
    {
77
        $client = static::createClient();
78
79
        $homepage = $this->createPage([
80
            'homepage' => true,
81
            'enabled'  => true,
82
            'locale'   => 'en',
83
            'slug'     => 'home',
84
            'title'    => 'My homepage',
85
            'host'     => 'localhost',
86
            'content'  => 'Hello world!',
87
        ]);
88
89
        /** @var EntityManager $em */
90
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
91
        $em->persist($homepage);
92
        $em->flush();
93
94
        $crawler = $client->request('GET', '/page/');
95
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
96
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
97
        static::assertContains($homepage->getContent(), trim($crawler->filter('article')->html()));
98
99
        // Repeat with the homepage directly in the url
100
101
        // First, check that any right trimming "/" will redirect
102
        $client->request('GET', '/page/home/');
103
        static::assertTrue($client->getResponse()->isRedirect('/page/home'));
104
        $client->followRedirect();
105
106
        // Check that the homepage with slug is redirected to the root page
107
        static::assertTrue($client->getResponse()->isRedirect('/page?_locale=en'));
108
        $crawler = $client->followRedirect();
109
110
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
111
        static::assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
112
        static::assertContains($homepage->getContent(), trim($crawler->filter('article')->html()));
113
    }
114
115
    public function testTree()
116
    {
117
        $client = static::createClient();
118
119
        /** @var EntityManager $em */
120
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
121
122
        // Prepare 3 pages : the root, the first level, and the third one that's disabled
123
        $parent = $this->createPage([
124
            'homepage' => true,
125
            'enabled'  => true,
126
            'slug'     => 'root',
127
            'title'    => 'Root',
128
            'content'  => 'The root page',
129
        ]);
130
        $em->persist($parent);
131
        $em->flush();
132
133
        $childOne = $this->createPage([
134
            'enabled' => true,
135
            'slug'    => 'first-level',
136
            'title'   => 'First level',
137
            'content' => 'This page is only available in the first level',
138
            'parent'  => $em->find(get_class($parent), $parent),
139
        ]);
140
        $em->persist($childOne);
141
        $em->flush();
142
143
        $childTwoDisabled = $this->createPage([
144
            'enabled' => false,
145
            'slug'    => 'second-level',
146
            'title'   => 'Disabled Page',
147
            'content' => 'This page should render a 404 error',
148
            'parent'  => $em->find(get_class($parent), $parent),
149
        ]);
150
        $em->persist($childTwoDisabled);
151
        $em->flush();
152
153
        // Repeat with the homepage directly in the url
154
        $crawler = $client->request('GET', '/page/'.$childOne->getTree());
155
        static::assertEquals($childOne->getTitle(), trim($crawler->filter('title')->html()));
156
        static::assertEquals($childOne->getTitle(), trim($crawler->filter('article > h1')->html()));
157
        static::assertContains($childOne->getContent(), trim($crawler->filter('article')->html()));
158
159
        // Repeat with the homepage directly in the url
160
        $client->request('GET', '/page/root/second-level');
161
        static::assertEquals(404, $client->getResponse()->getStatusCode());
162
    }
163
164
    public function testMetas()
165
    {
166
        $client = static::createClient();
167
168
        /** @var EntityManager $em */
169
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
170
171
        $page = $this->createPage([
172
            'homepage'        => true,
173
            'enabled'         => true,
174
            'title'           => 'Root',
175
            'content'         => 'The root page',
176
            'css'             => '#home{color:red;}',
177
            'js'              => 'alert("ok");',
178
            'metaDescription' => 'meta description',
179
            'metaKeywords'    => 'this is a meta keyword list',
180
            'metaTitle'       => 'this title is only in the metas',
181
        ]);
182
        $em->persist($page);
183
        $em->flush();
184
185
        $crawler = $client->request('GET', '/page');
186
        static::assertEquals($page->getTitle(), trim($crawler->filter('title')->html()));
187
        static::assertEquals($page->getTitle(), trim($crawler->filter('article > h1')->html()));
188
        static::assertContains($page->getContent(), trim($crawler->filter('article')->html()));
189
190
        static::assertEquals($page->getCss(), trim($crawler->filter('#orbitale_cms_css')->html()));
191
        static::assertEquals($page->getJs(), trim($crawler->filter('#orbitale_cms_js')->html()));
192
        static::assertEquals($page->getMetaDescription(), trim($crawler->filter('meta[name="description"]')
193
            ->attr('content')));
194
        static::assertEquals($page->getMetaKeywords(), trim($crawler->filter('meta[name="keywords"]')
195
            ->attr('content')));
196
        static::assertEquals($page->getMetaTitle(), trim($crawler->filter('meta[name="title"]')->attr('content')));
197
    }
198
199
    public function testParentAndChildrenDontReverse()
200
    {
201
        $client = static::createClient();
202
        /** @var EntityManager $em */
203
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
204
205
        $parent = $this->createPage([
206
            'enabled'  => true,
207
            'homepage' => true,
208
            'title'    => 'Locale+host',
209
            'host'     => 'localhost',
210
            'locale'   => 'en',
211
        ]);
212
        $em->persist($parent);
213
        $em->flush();
214
215
        $child = $this->createPage([
216
            'enabled'  => true,
217
            'homepage' => true,
218
            'title'    => 'Host only',
219
            'host'     => 'localhost',
220
            'parent'   => $parent,
221
        ]);
222
        $em->persist($child);
223
        $em->flush();
224
225
        $client->request('GET', '/page/'.$child->getSlug().'/'.$parent->getSlug());
226
        static::assertEquals(404, $client->getResponse()->getStatusCode());
227
    }
228
229
    /**
230
     * With the locale & host matching system, the precedence of the homepage should have this order (the first being the most important):
231
     * - Locale & Host
232
     * - Host only
233
     * - Locale only
234
     * - No matching criteria
235
     * If there are multiple pages that match any "matching criteria", the behavior is unexpected, so we should not handle this naturally.
236
     */
237
    public function testAllTypesOfPagesForHomepage()
238
    {
239
        $client = static::createClient();
240
241
        /** @var EntityManager $em */
242
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
243
244
        // First, create the pages
245
        /** @var Page[] $pages */
246
        $pages = [
247
            'both'   => $this->createPage([
248
                'enabled'  => true,
249
                'homepage' => true,
250
                'title'    => 'Locale+host',
251
                'host'     => 'localhost',
252
                'locale'   => 'en',
253
            ]),
254
            'host'   => $this->createPage([
255
                'enabled'  => true,
256
                'homepage' => true,
257
                'title'    => 'Host only',
258
                'host'     => 'localhost',
259
            ]),
260
            'locale' => $this->createPage([
261
                'enabled'  => true,
262
                'homepage' => true,
263
                'title'    => 'Locale only',
264
                'locale'   => 'en',
265
            ]),
266
            'none'   => $this->createPage([
267
                'enabled'  => true,
268
                'homepage' => true,
269
                'title'    => 'No match',
270
            ]),
271
        ];
272
        foreach ($pages as $page) {
273
            $em->persist($page);
274
        }
275
        $em->flush();
276
277
        // First page considered as homepage is the last one inserted.
278
        $pages = array_reverse($pages);
279
280
        // Loop the pages because the "$pages" array respects precedence,
281
        // So disabling the pages on each loop should make all assertions work.
282
        foreach ($pages as $key => $page) {
283
            $crawler = $client->request('GET', '/page/');
284
            static::assertEquals($page->getTitle(), trim($crawler->filter('title')->html()));
285
            $page->setEnabled(false);
286
            $em->merge($page);
287
            $em->flush();
288
        }
289
    }
290
291
    public function testBreadcrumbsDesign()
292
    {
293
        $client = static::createClient(['environment' => 'design_breadcrumbs']);
294
295
        /** @var EntityManager $em */
296
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
297
298
        $page = $this->createPage(['enabled' => true, 'slug' => 'parent', 'title' => 'Parent page']);
299
        $em->persist($page);
300
        $em->flush();
301
        $pageChild = $this->createPage([
302
            'enabled' => true,
303
            'slug'    => 'child',
304
            'title'   => 'Child page',
305
            'parent'  => $page,
306
        ]);
307
        $em->persist($pageChild);
308
        $em->flush();
309
310
        static::assertEquals('parent/child', $pageChild->getTree());
311
        $crawler = $client->request('GET', '/page/'.$pageChild->getTree());
312
313
        static::assertEquals('breadcrumb-test-class', $crawler->filter('#breadcrumbs')->first()->attr('class'));
314
315
        $nodes = $crawler->filter('#breadcrumbs *');
316
317
        /** @var \DOMElement[] $nodesArray */
318
        $nodesArray = [];
319
        foreach ($nodes as $k => $node) {
320
            // This is a trick for SF2.3 and the lack of "getNode" method.
321
            $nodesArray[$k] = $node;
322
        }
323
324
        // First element = homepage
325
        $homeNode = $nodesArray[0];
326
        static::assertEquals('a', $homeNode->tagName);
327
        static::assertEquals('breadcrumb-link', $homeNode->getAttribute('class'));
328
329
        // Second element = separator
330
        $separator = $nodesArray[1];
331
        static::assertEquals('span', $separator->tagName);
332
        static::assertEquals('breadcrumb-overriden-separator-class', $separator->getAttribute('class'));
333
        static::assertEquals('|', $separator->textContent);
334
335
        // Third element = link to the parent page
336
        $firstLinkNode = $nodesArray[2];
337
        static::assertEquals('a', $firstLinkNode->tagName);
338
        static::assertEquals('breadcrumb-link', $firstLinkNode->getAttribute('class'));
339
        static::assertEquals($page->getTitle(), trim($firstLinkNode->textContent));
340
341
        // We sort of skip node 3 because it should be a separator
342
        static::assertEquals('breadcrumb-overriden-separator-class', $nodesArray[3]->getAttribute('class'));
343
344
        $currentLinkNode = $nodesArray[4];
345
        static::assertEquals('span', $currentLinkNode->tagName);
346
        static::assertEquals('breadcrumb-current', $currentLinkNode->getAttribute('class'));
347
        static::assertEquals($pageChild->getTitle(), trim($currentLinkNode->textContent));
348
349
        $crawler->clear();
350
    }
351
}
352