Completed
Push — master ( 28ddb0...754cfb )
by Alex
03:13
created

PageControllerTest::testOneHomepageWithLocale()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 25

Duplication

Lines 39
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 39
loc 39
rs 8.8571
cc 1
eloc 25
nc 1
nop 0
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\Entity\Page;
16
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\AbstractTestCase;
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
        $this->assertEquals($error, trim($crawler->filter('title')->html()));
26
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
27
    }
28
29
    public function testNoPageWithSlug()
30
    {
31
        $client = static::createClient();
32
        $client->request('GET', '/page/inexistent-slug');
33
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
34
    }
35
36 View Code Duplication
    public function testOneHomepage()
37
    {
38
        $client = static::createClient();
39
40
        $homepage = $this->createPage(array(
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
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
56
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
57
        $this->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
        $this->assertTrue($client->getResponse()->isRedirect('/page/home'));
64
        $client->followRedirect();
65
66
        // Check that the homepage with slug is redirected to the root page
67
        $this->assertTrue($client->getResponse()->isRedirect('/page'));
68
        $crawler = $client->followRedirect();
69
70
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
71
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
72
        $this->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(array(
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
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
96
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
97
        $this->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
        $this->assertTrue($client->getResponse()->isRedirect('/page/home'));
104
        $client->followRedirect();
105
106
        // Check that the homepage with slug is redirected to the root page
107
        $this->assertTrue($client->getResponse()->isRedirect('/page?_locale=en'));
108
        $crawler = $client->followRedirect();
109
110
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('title')->html()));
111
        $this->assertEquals($homepage->getTitle(), trim($crawler->filter('article > h1')->html()));
112
        $this->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(array(
124
            'homepage' => true,
125
            'enabled' => true,
126
            'slug' => 'root',
127
            'title' => 'Root',
128
            'content' => 'The root page',
129
            'deletedAt' => null,
130
        ));
131
        $em->persist($parent);
132
        $em->flush();
133
134
        $childOne = $this->createPage(array(
135
            'enabled' => true,
136
            'slug' => 'first-level',
137
            'title' => 'First level',
138
            'content' => 'This page is only available in the first level',
139
            'parent' => $em->find(get_class($parent), $parent),
140
            'deletedAt' => null,
141
        ));
142
        $em->persist($childOne);
143
        $em->flush();
144
145
        $childTwoDisabled = $this->createPage(array(
146
            'enabled' => false,
147
            'slug' => 'second-level',
148
            'title' => 'Disabled Page',
149
            'content' => 'This page should render a 404 error',
150
            'parent' => $em->find(get_class($parent), $parent),
151
            'deletedAt' => null,
152
        ));
153
        $em->persist($childTwoDisabled);
154
        $em->flush();
155
156
        // Repeat with the homepage directly in the url
157
        $crawler = $client->request('GET', '/page/'.$childOne->getTree());
158
        $this->assertEquals($childOne->getTitle(), trim($crawler->filter('title')->html()));
159
        $this->assertEquals($childOne->getTitle(), trim($crawler->filter('article > h1')->html()));
160
        $this->assertContains($childOne->getContent(), trim($crawler->filter('article')->html()));
161
162
        // Repeat with the homepage directly in the url
163
        $client->request('GET', '/page/root/second-level');
164
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
165
    }
166
167
    public function testMetas()
168
    {
169
        $client = static::createClient();
170
171
        /** @var EntityManager $em */
172
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
173
174
        $page = $this->createPage(array(
175
            'homepage' => true,
176
            'enabled' => true,
177
            'title' => 'Root',
178
            'content' => 'The root page',
179
            'css' => '#home{color:red;}',
180
            'js' => 'alert("ok");',
181
            'metaDescription' => 'meta description',
182
            'metaKeywords' => 'this is a meta keyword list',
183
            'metaTitle' => 'this title is only in the metas',
184
        ));
185
        $em->persist($page);
186
        $em->flush();
187
188
        $crawler = $client->request('GET', '/page');
189
        $this->assertEquals($page->getTitle(), trim($crawler->filter('title')->html()));
190
        $this->assertEquals($page->getTitle(), trim($crawler->filter('article > h1')->html()));
191
        $this->assertContains($page->getContent(), trim($crawler->filter('article')->html()));
192
193
        $this->assertEquals($page->getCss(), trim($crawler->filter('#orbitale_cms_css')->html()));
194
        $this->assertEquals($page->getJs(), trim($crawler->filter('#orbitale_cms_js')->html()));
195
        $this->assertEquals($page->getMetaDescription(), trim($crawler->filter('meta[name="description"]')->attr('content')));
196
        $this->assertEquals($page->getMetaKeywords(), trim($crawler->filter('meta[name="keywords"]')->attr('content')));
197
        $this->assertEquals($page->getMetaTitle(), trim($crawler->filter('meta[name="title"]')->attr('content')));
198
    }
199
200
    public function testParentAndChildrenDontReverse()
201
    {
202
        $client = static::createClient();
203
        /** @var EntityManager $em */
204
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
205
206
        $parent = $this->createPage(array('enabled' => true, 'homepage' => true, 'title' => 'Locale+host', 'host' => 'localhost', 'locale' => 'en'));
207
        $em->persist($parent);
208
        $em->flush();
209
210
        $child = $this->createPage(array('enabled' => true, 'homepage' => true, 'title' => 'Host only', 'host' => 'localhost', 'parent' => $parent));
211
        $em->persist($child);
212
        $em->flush();
213
214
        $client->request('GET', '/page/'.$child->getSlug().'/'.$parent->getSlug());
215
        $this->assertEquals(404, $client->getResponse()->getStatusCode());
216
    }
217
218
    /**
219
     * With the locale & host matching system, the precedence of the homepage should have this order (the first being the most important):
220
     * - Locale & Host
221
     * - Host only
222
     * - Locale only
223
     * - No matching criteria
224
     * If there are multiple pages that match any "matching criteria", the behavior is unexpected, so we should not handle this naturally.
225
     */
226
    public function testAllTypesOfPagesForHomepage()
227
    {
228
        $client = static::createClient();
229
230
        /** @var EntityManager $em */
231
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
232
233
        // First, create the pages
234
        /** @var Page[] $pages */
235
        $pages = array(
236
            'both' => $this->createPage(array('enabled' => true, 'homepage' => true, 'title' => 'Locale+host', 'host' => 'localhost', 'locale' => 'en')),
237
            'host' => $this->createPage(array('enabled' => true, 'homepage' => true, 'title' => 'Host only', 'host' => 'localhost')),
238
            'locale' => $this->createPage(array('enabled' => true, 'homepage' => true, 'title' => 'Locale only', 'locale' => 'en')),
239
            'none' => $this->createPage(array('enabled' => true, 'homepage' => true, 'title' => 'No match')),
240
        );
241
        foreach ($pages as $page) {
242
            $em->persist($page);
243
        }
244
        $em->flush();
245
246
        // Loop the pages because the "$pages" array respects precedence,
247
        // So disabling the pages on each loop should make all assertions work.
248
        foreach ($pages as $key => $page) {
249
            $crawler = $client->request('GET', '/page/');
250
            $this->assertEquals($page->getTitle(), trim($crawler->filter('title')->html()));
251
            $page->setEnabled(false);
252
            $em->merge($page);
253
            $em->flush();
254
        }
255
    }
256
257
    public function testBreadcrumbsDesign()
258
    {
259
        $client = static::createClient(array('environment' => 'design_breadcrumbs'));
260
261
        /** @var EntityManager $em */
262
        $em = $client->getKernel()->getContainer()->get('doctrine')->getManager();
263
264
        $page = $this->createPage(array('enabled' => true, 'slug' => 'parent', 'title' => 'Parent page'));
265
        $em->persist($page);
266
        $em->flush();
267
        $pageChild = $this->createPage(array('enabled' => true, 'slug' => 'child', 'title' => 'Child page', 'parent' => $page));
268
        $em->persist($pageChild);
269
        $em->flush();
270
271
        $this->assertEquals('parent/child', $pageChild->getTree());
272
        $crawler = $client->request('GET', '/page/'.$pageChild->getTree());
273
274
        $this->assertEquals('breadcrumb-test-class', $crawler->filter('#breadcrumbs')->first()->attr('class'));
275
276
        $nodes = $crawler->filter('#breadcrumbs *');
277
278
        /** @var \DOMElement[] $nodesArray */
279
        $nodesArray = array();
280
        foreach ($nodes as $k => $node) {
281
            // This is a trick for SF2.3 and the lack of "getNode" method.
282
            $nodesArray[$k] = $node;
283
        }
284
285
        // First element = homepage
286
        $homeNode = $nodesArray[0];
287
        $this->assertEquals('a', $homeNode->tagName);
288
        $this->assertEquals('breadcrumb-link', $homeNode->getAttribute('class'));
289
290
        // Second element = separator
291
        $separator = $nodesArray[1];
292
        $this->assertEquals('span', $separator->tagName);
293
        $this->assertEquals('breadcrumb-overriden-separator-class', $separator->getAttribute('class'));
294
        $this->assertEquals('|', $separator->textContent);
295
296
        // Third element = link to the parent page
297
        $firstLinkNode = $nodesArray[2];
298
        $this->assertEquals('a', $firstLinkNode->tagName);
299
        $this->assertEquals('breadcrumb-link', $firstLinkNode->getAttribute('class'));
300
        $this->assertEquals($page->getTitle(), trim($firstLinkNode->textContent));
301
302
        // We sort of skip node 3 because it should be a separator
303
        $this->assertEquals('breadcrumb-overriden-separator-class', $nodesArray[3]->getAttribute('class'));
304
305
        $currentLinkNode = $nodesArray[4];
306
        $this->assertEquals('span', $currentLinkNode->tagName);
307
        $this->assertEquals('breadcrumb-current', $currentLinkNode->getAttribute('class'));
308
        $this->assertEquals($pageChild->getTitle(), trim($currentLinkNode->textContent));
309
310
        $crawler->clear();
311
    }
312
}
313