Source of file GreenTest.php
Size: 8,824 Bytes - Last Modified: 2021-12-23T10:56:30+00:00
/var/www/docs.ssmods.com/process/src/tests/GreenTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 | <?php use Symfony\Component\Filesystem\Filesystem; use UncleCheese\Green\Green; use UncleCheese\Green\DesignModule; use UncleCheese\Green\DataSource; class GreenTest extends SapphireTest { protected $fs; protected $extraDataObjects = [ 'TestGreenPage' ]; private static $sample_files = [ 'config.yml', 'content.yml', 'index.ss', 'sample.css', 'another-sample.js', 'someimage.jpeg' ]; public function setUp() { $this->fs = new Filesystem(); foreach(self::$sample_files as $file) { $this->fs->copy( $this->getAssetPath($file), $this->getFileTestPath($file) ); } Config::inst()->update( 'UncleCheese\Green\Green', 'design_folder', GREEN_DIR.'/tests/green-test' ); } public function tearDown() { $this->fs->remove( $this->getFileTestPath() ); } public function testDataSourceDBObject() { $dataSource = $this->createDataSource(); $dbObj = $dataSource->toDBObject(); $this->assertEquals('test title', $dbObj->Title); $this->assertTrue($dbObj->obj('Date') instanceof Date); } public function testDataSourceImages() { $dataSource = $this->createDataSource(); $dataObj = $dataSource->toDBObject(); $this->assertTrue( $dataObj->obj('SomeImage') instanceof Image_Cached ); } public function testDataSourceGetters() { $dataSource = $this->createDataSource(); $this->assertEquals('content.yml', $dataSource->getName()); $this->assertEquals(__DIR__.'/green-test/test-design/content.yml', $dataSource->getPath()); } public function testDesignModuleGetters() { $designModule = $this->createDesignModule(); $this->assertEquals( $this->getFileTestPath(), $designModule->getPath() ); $this->assertEquals( GREEN_DIR.'/tests/green-test/test-design', $designModule->getRelativePath() ); $this->assertEquals( 'test-design', $designModule->getName() ); $this->assertEquals( $this->getFileTestPath('index.ss'), $designModule->getLayoutTemplateFile() ); $this->assertEquals( file_get_contents($this->getFileTestPath('index.ss')), $designModule->getTemplateContents() ); $this->fs->dumpFile( $this->getFileTestPath('main.ss'), 'test' ); $this->assertEquals( $this->getFileTestPath('main.ss'), $designModule->getMainTemplateFile() ); $this->fs->remove( $this->getFileTestPath('main.ss') ); $this->fs->remove( $this->getFileTestPath('index.ss') ); $this->fs->dumpFile( $this->getFileTestPath('layout.ss'), 'test' ); $this->assertEquals( $this->getFileTestPath('layout.ss'), $designModule->getLayoutTemplateFile() ); } public function testDesignModuleCreatesDataSource() { $designModule = $this->createDesignModule(); $dataSource = $designModule->getDataSource(); $this->assertTrue($dataSource instanceof DataSource); $this->fs->remove($this->getFileTestPath('content.yml')); $this->fs->touch($this->getFileTestPath('content.json')); $this->assertTrue($dataSource instanceof DataSource); } public function testDesignModuleReturnsFalseOnNoDataSource() { $designModule = $this->createDesignModule(); $this->fs->remove($this->getFileTestPath('content.yml')); $this->assertFalse($designModule->getDataSource()); } public function testDesignModuleGetsConfiguration() { $designModule = $this->createDesignModule(); $config = $designModule->getConfiguration(); $this->assertTrue($config instanceof YAMLField); $this->assertEquals('bar', (string) $config->foo); } public function testDesignModuleReturnsArrayDataOnNoConfiguration() { $designModule = $this->createDesignModule(); $this->fs->remove($this->getFileTestPath('config.yml')); $config = $designModule->getConfiguration(); $this->assertTrue($config instanceof ArrayData); } public function testDesignModuleAssets() { $designModule = $this->createDesignModule(); $this->assertCount(1, $designModule->getStylesheets()); $this->assertEquals( $this->getRelativeFileTestPath('sample.css'), $designModule->getStylesheets()[0] ); $this->assertCount(1, $designModule->getJavascripts()); $this->assertEquals( $this->getRelativeFileTestPath('another-sample.js'), $designModule->getJavascripts()[0] ); $this->assertCount(1, $designModule->getImages()); $this->assertEquals( $this->getRelativeFileTestPath('someimage.jpeg'), $designModule->getImages()[0] ); } public function testDesignModuleRequirements() { $mock = $this->getMockBuilder('Requirements_Backend') ->setMethods(['javascript','css']) ->getMock(); $mock->expects($this->once()) ->method('css') ->with($this->getRelativeFileTestPath('sample.css')); $mock->expects($this->once()) ->method('javascript') ->with($this->getRelativeFileTestPath('another-sample.js')); Requirements::set_backend($mock); $this->createDesignModule()->loadRequirements(); } public function testGreenGetters() { $green = Green::inst(); $this->assertEquals( GREEN_DIR.'/tests/green-test', $green->getDesignFolder() ); $this->assertEquals( BASE_PATH.'/'.GREEN_DIR.'/tests/green-test', $green->getAbsoluteDesignFolder() ); } public function testGreenGetsDesignModules() { $green = Green::inst(); $modules = $green->getDesignModules(); $this->assertCount(1, $modules); $this->assertTrue($modules[0] instanceof DesignModule); $this->fs->mkdir(__DIR__.'/green-test/dummy'); $modules = $green->getDesignModules(); $this->assertCount(1, $modules); $module = $green->getDesignModule('test-design'); $this->assertTrue($module instanceof DesignModule); $this->assertFalse($green->getDesignModule('dummy')); } public function testGetCMSFieldsAddsCodeEditorOnlyWhenNeeded() { $page = TestGreenPage::create([ 'DesignModule' => 'test-design' ]); $page->write(); $fields = $page->getCMSFields(); $this->assertNull($fields->dataFieldByName('TemplateData')); $this->fs->remove($this->getFileTestPath('content.yml')); $fields = $page->getCMSFields(); $editor = Injector::inst()->get('YAMLEditor'); $this->assertTrue( $fields->dataFieldByName('TemplateData') instanceof $editor->class ); } public function testGreenPageRenderingFromCMSWithFile() { $page = TestGreenPage::create([ 'DesignModule' => 'test-design', 'URLSegment' => 'tester', 'ParentID' => 0 ]); $page->write(); $page->publish("Stage", "Live"); $response = Director::test('tester'); $body = $response->getBody(); $this->assertRegExp( '/Title\: test title/', $body ); $this->assertRegExp( '/Date\: 01\/01\/2015/', $body ); } public function testGreenPageRenderingFromCMSWithInlineData() { $page = TestGreenPage::create([ 'DesignModule' => 'test-design', 'URLSegment' => 'tester-2', 'ParentID' => 0 ]); $this->fs->remove( $this->getFileTestPath('content.yml') ); $page->TemplateData = <<<YAML Title: title changed Date: Date|2015-02-02 YAML; $page->write(); $page->publish("Stage", "Live"); $response = Director::test('tester-2'); $body = $response->getBody(); $this->assertRegExp( '/Title\: title changed/', $body ); $this->assertRegExp( '/Date\: 02\/02\/2015/', $body ); } public function testGreenPageRenderingFromPublicURL() { $response = Director::test('test-url'); $this->assertEquals(404, $response->getStatusCode()); $this->fs->dumpFile( $this->getFileTestPath('config.yml'), 'public_url: test-url' ); Green::install(); $response = Director::test('test-url'); $this->assertEquals(200, $response->getStatusCode()); $body = $response->getBody(); $this->assertRegExp( '/Title\: test title/', $body ); $this->assertRegExp( '/Date\: 01\/01\/2015/', $body ); $this->fs->dumpFile( $this->getFileTestPath('main.ss'), "___MAIN___\n\$Layout" ); $response = Director::test('test-url'); $this->assertEquals(200, $response->getStatusCode()); $body = $response->getBody(); $this->assertEquals( '___MAIN___', substr($body, 0, 10) ); $this->assertRegExp( '/Title\: test title/', $body ); $this->assertRegExp( '/Date\: 01\/01\/2015/', $body ); } protected function getFileTestPath($file = null) { return __DIR__.'/green-test/test-design' . ($file ? "/$file" : ''); } protected function getRelativeFileTestPath($file = null) { return GREEN_DIR.'/tests/green-test/test-design' . ($file ? "/$file" : ''); } protected function getAssetPath($file = null) { return __DIR__ . '/_assets/' . ($file ? "/$file" : ''); } protected function createDesignModule() { return new DesignModule(__DIR__.'/green-test/test-design'); } protected function createDataSource() { return new DataSource(__DIR__.'/green-test/test-design/content.yml'); } } |