template = new StringTemplate(); } /** * Test adding templates through the constructor. * * @return void */ public function testConstructorAdd() { $templates = [ 'link' => '{{text}}' ]; $template = new StringTemplate($templates); $this->assertEquals($templates['link'], $template->get('link')); } /** * test adding templates. * * @return void */ public function testAdd() { $templates = [ 'link' => '{{text}}' ]; $result = $this->template->add($templates); $this->assertSame( $this->template, $result, 'The same instance should be returned' ); $this->assertEquals($templates['link'], $this->template->get('link')); } /** * Test remove. * * @return void */ public function testRemove() { $templates = [ 'link' => '{{text}}' ]; $this->template->add($templates); $this->assertNull($this->template->remove('link'), 'No return'); $this->assertNull($this->template->get('link'), 'Template should be gone.'); } /** * Test formatting strings. * * @return void */ public function testFormat() { $templates = [ 'link' => '{{text}}' ]; $this->template->add($templates); $result = $this->template->format('not there', []); $this->assertSame('', $result); $result = $this->template->format('link', [ 'url' => '/', 'text' => 'example' ]); $this->assertEquals('example', $result); } /** * Test loading templates files in the app. * * @return void */ public function testLoad() { $this->template->remove('attribute'); $this->template->remove('compactAttribute'); $this->assertEquals([], $this->template->get()); $this->assertNull($this->template->load('test_templates')); $this->assertEquals('{{text}}', $this->template->get('link')); } /** * Test loading templates files from a plugin * * @return void */ public function testLoadPlugin() { Plugin::load('TestPlugin'); $this->assertNull($this->template->load('TestPlugin.test_templates')); $this->assertEquals('{{text}}', $this->template->get('italic')); } /** * Test that loading non-existing templates causes errors. * * @expectedException \Cake\Core\Exception\Exception * @expectedExceptionMessage Could not load configuration file */ public function testLoadErrorNoFile() { $this->template->load('no_such_file'); } /** * Test formatting compact attributes. * * @return void */ public function testFormatAttributesCompact() { $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple']; $result = $this->template->formatAttributes($attrs); $this->assertEquals( ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"', $result ); $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null]; $result = $this->template->formatAttributes($attrs); $this->assertEquals( '', $result ); } /** * Test formatting normal attributes. * * @return void */ public function testFormatAttributes() { $attrs = ['name' => 'bruce', 'data-hero' => '']; $result = $this->template->formatAttributes($attrs); $this->assertEquals( ' name="bruce" data-hero="<batman>"', $result ); $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '']; $result = $this->template->formatAttributes($attrs); $this->assertEquals( ' name="bruce" data-hero=""', $result ); $attrs = ['name' => 'bruce', 'data-hero' => '']; $result = $this->template->formatAttributes($attrs, ['name']); $this->assertEquals( ' data-hero="<batman>"', $result ); } /** * Test formatting array attributes. * * @return void */ public function testFormatAttributesArray() { $attrs = ['name' => ['bruce', 'wayne']]; $result = $this->template->formatAttributes($attrs); $this->assertEquals( ' name="bruce wayne"', $result ); } /** * test push/pop templates. * * @return void */ public function testPushPopTemplates() { $this->template->add(['name' => '{{name}} is my name']); $this->assertNull($this->template->push()); $this->template->add(['name' => 'my name']); $this->assertEquals('my name', $this->template->get('name')); $this->assertNull($this->template->pop()); $this->assertEquals('{{name}} is my name', $this->template->get('name')); $this->assertNull($this->template->pop()); $this->assertNull($this->template->pop()); } }