ORIENT/tests/unit/cms/classes/ControllerTest.php

370 lines
13 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php
use Cms\Classes\Theme;
2014-07-29 07:44:24 +00:00
use Cms\Classes\Controller;
2014-05-14 13:24:20 +00:00
class ControllerTest extends TestCase
{
public function test404()
{
/*
* Test the built-in 404 page
*/
$theme = new Theme();
$theme->load('apitest');
$controller = new Controller($theme);
$response = $controller->run('/some-page-that-doesnt-exist');
$this->assertNotEmpty($response);
$this->assertInstanceOf('\Illuminate\Http\Response', $response);
ob_start();
include base_path().'/modules/cms/views/404.php';
$page404Content = ob_get_contents();
ob_end_clean();
$this->assertEquals($page404Content, $response->getContent());
/*
* Test the theme 404 page
*/
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/some-page-that-doesnt-exist');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getContent();
$this->assertInternalType('string', $content);
$this->assertEquals('<p>Page not found</p>', $content);
2014-05-14 13:24:20 +00:00
}
public function testRoot()
{
/*
* Test the / route and the fallback layout
*/
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getContent();
$this->assertInternalType('string', $content);
$this->assertEquals('<h1>My Webpage</h1>', trim($content));
2014-05-14 13:24:20 +00:00
}
/**
* @expectedException Cms\Classes\CmsException
* @expectedExceptionMessage is not found
*/
public function testLayoutNotFound()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/no-layout');
}
public function testExistingLayout()
{
/*
* Test existing layout
*/
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-layout');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getContent();
$this->assertEquals('<div><p>Hey</p></div>', $content);
2014-05-14 13:24:20 +00:00
}
public function testPartials()
{
/*
* Test partials referred in the layout and page
*/
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-partials')->getContent();
2014-05-14 13:24:20 +00:00
$this->assertEquals('<div>LAYOUT PARTIAL<p>Hey PAGE PARTIAL Homer Simpson A partial</p></div>', $response);
}
public function testContent()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-content')->getContent();
2014-05-14 13:24:20 +00:00
$this->assertEquals('<div>LAYOUT CONTENT<p>Hey PAGE CONTENT A content</p></div>', $response);
}
public function testBlocks()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-placeholder')->getContent();
2014-05-14 13:24:20 +00:00
$this->assertEquals("<div>LAYOUT CONTENT <span>BLOCK\n DEFAULT</span> <p>Hey PAGE CONTENT</p></div>SECOND BLOCK", $response);
}
public function testLayoutInSubdirectory()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/apage')->getContent();
2014-05-14 13:24:20 +00:00
$this->assertEquals("<div>LAYOUT CONTENT <h1>This page is a subdirectory</h1></div>", $response);
}
/**
* @expectedException Cms\Classes\CmsException
* @expectedExceptionMessage is not found
*/
public function testPartialNotFound()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/no-partial')->getContent();
2014-05-14 13:24:20 +00:00
}
public function testPageLifeCycle()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/cycle-test')->getContent();
2014-05-14 13:24:20 +00:00
$this->assertEquals('12345', $response);
}
protected function configAjaxRequestMock($handler, $partials = false)
{
$requestMock = $this->getMock('Illuminate\Http\Request', array('header'));
$requestMock->expects($this->at(0))->
method('header')->
with($this->stringContains('X_OCTOBER_REQUEST_HANDLER'), $this->anything())->
will($this->returnValue($handler));
2014-07-28 22:55:46 +00:00
if ($partials !== false)
2014-05-14 13:24:20 +00:00
$requestMock->expects($this->at(1))->
method('header')->
with($this->stringContains('X_OCTOBER_REQUEST_PARTIALS'), $this->anything())->
will($this->returnValue($partials));
return $requestMock;
}
public function testAjaxHandlerNotFound()
{
App::instance('request', $this->configAjaxRequestMock('onNoHandler', ''));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertInternalType('string', $response->getOriginalContent());
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals("AJAX handler 'onNoHandler' was not found.", $response->getOriginalContent());
}
public function testAjaxInvalidHandlerName()
{
App::instance('request', $this->configAjaxRequestMock('delete'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertInternalType('string', $response->getOriginalContent());
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('Invalid AJAX handler name: delete.', $response->getOriginalContent());
}
public function testAjaxInvalidPartial()
{
App::instance('request', $this->configAjaxRequestMock('onTest', 'p:artial'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertInternalType('string', $response->getOriginalContent());
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('Invalid partial name: p:artial.', $response->getOriginalContent());
}
public function testAjaxPartialNotFound()
{
App::instance('request', $this->configAjaxRequestMock('onTest', 'partial'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertInternalType('string', $response->getOriginalContent());
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals("The partial 'partial' is not found.", $response->getOriginalContent());
}
public function testPageAjax()
{
App::instance('request', $this->configAjaxRequestMock('onTest', 'ajax-result'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getOriginalContent();
$this->assertInternalType('array', $content);
$this->assertEquals(200, $response->getStatusCode());
$this->assertCount(1, $content);
$this->assertArrayHasKey('ajax-result', $content);
$this->assertEquals('page', $content['ajax-result']);
}
public function testLayoutAjax()
{
App::instance('request', $this->configAjaxRequestMock('onTestLayout', 'ajax-result'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getOriginalContent();
$this->assertInternalType('array', $content);
$this->assertEquals(200, $response->getStatusCode());
$this->assertCount(1, $content);
$this->assertArrayHasKey('ajax-result', $content);
$this->assertEquals('layout-test', $content['ajax-result']);
}
public function testAjaxMultiplePartials()
{
App::instance('request', $this->configAjaxRequestMock('onTest', 'ajax-result&ajax-second-result'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/ajax-test');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getOriginalContent();
$this->assertInternalType('array', $content);
$this->assertEquals(200, $response->getStatusCode());
$this->assertCount(2, $content);
$this->assertArrayHasKey('ajax-result', $content);
$this->assertArrayHasKey('ajax-second-result', $content);
$this->assertEquals('page', $content['ajax-result']);
$this->assertEquals('second', $content['ajax-second-result']);
}
public function testBasicComponents()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-component')->getContent();
2014-05-14 13:24:20 +00:00
$page = PHPUnit_Framework_Assert::readAttribute($controller, 'page');
$this->assertArrayHasKey('testArchive', $page->components);
2014-05-14 13:24:20 +00:00
$component = $page->components['testArchive'];
$details = $component->componentDetails();
$content = <<<ESC
<div>LAYOUT CONTENT<p>This page uses components.</p>
<h3>Lorum ipsum</h3>
<p>Post Content #1</p>
<h3>La Playa Nudista</h3>
<p>Second Post Content</p>
</div>
ESC;
$this->assertEquals($content, $response);
$this->assertEquals(69, $component->property('posts-per-page'));
$this->assertEquals('Blog Archive Dummy Component', $details['name']);
$this->assertEquals('Displays an archive of blog posts.', $details['description']);
}
public function testComponentAliases()
{
2014-09-15 21:36:05 +00:00
include_once base_path() . '/tests/fixtures/system/plugins/october/test/components/Archive.php';
2014-05-14 13:24:20 +00:00
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-components')->getContent();
2014-05-14 13:24:20 +00:00
$page = PHPUnit_Framework_Assert::readAttribute($controller, 'page');
$this->assertArrayHasKey('firstAlias', $page->components);
$this->assertArrayHasKey('secondAlias', $page->components);
2014-07-28 22:55:46 +00:00
2014-05-14 13:24:20 +00:00
$component = $page->components['firstAlias'];
$component2 = $page->components['secondAlias'];
$content = <<<ESC
<div>LAYOUT CONTENT<p>This page uses components.</p>
<h3>Lorum ipsum</h3>
<p>Post Content #1</p>
<h3>La Playa Nudista</h3>
<p>Second Post Content</p>
</div>
ESC;
$this->assertEquals($content, $response);
$this->assertEquals(6, $component->property('posts-per-page'));
$this->assertEquals(9, $component2->property('posts-per-page'));
}
public function testComponentAjax()
{
App::instance('request', $this->configAjaxRequestMock('testArchive::onTestAjax', 'ajax-result'));
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$response = $controller->run('/with-component');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$content = $response->getOriginalContent();
$this->assertInternalType('array', $content);
$this->assertEquals(200, $response->getStatusCode());
$this->assertCount(1, $content);
$this->assertArrayHasKey('ajax-result', $content);
$this->assertEquals('page', $content['ajax-result']);
}
public function testThemeUrl()
{
$theme = new Theme();
$theme->load('test');
$controller = new Controller($theme);
$url = $controller->themeUrl();
$this->assertEquals('/tests/fixtures/cms/themes/test', $url);
$url = $controller->themeUrl('foo/bar.css');
$this->assertEquals('/tests/fixtures/cms/themes/test/foo/bar.css', $url);
$url = $controller->themeUrl(['assets/css/style1.css', 'assets/css/style2.css']);
$url = substr($url, 0, strpos($url, '-'));
2014-09-15 21:36:05 +00:00
$this->assertEquals('/combine/adec0b826d103d671ab88a38b65734d7', $url);
$url = $controller->themeUrl(['assets/js/script1.js', 'assets/js/script2.js']);
$url = substr($url, 0, strpos($url, '-'));
2014-09-15 21:36:05 +00:00
$this->assertEquals('/combine/0b07ee67e15e6985d289c34b67d1a8e2', $url);
}
}