public function testLinkShortcodeHandler()
 {
     $testFile = $this->objFromFixture('File', 'asdf');
     $parser = new ShortcodeParser();
     $parser->register('file_link', array('File', 'link_shortcode_handler'));
     $fileShortcode = sprintf('[file_link,id=%d]', $testFile->ID);
     $fileEnclosed = sprintf('[file_link,id=%d]Example Content[/file_link]', $testFile->ID);
     $fileShortcodeExpected = $testFile->Link();
     $fileEnclosedExpected = sprintf('<a href="%s" class="file" data-type="txt" data-size="977 KB">Example Content</a>', $testFile->Link());
     $this->assertEquals($fileShortcodeExpected, $parser->parse($fileShortcode), 'Test that simple linking works.');
     $this->assertEquals($fileEnclosedExpected, $parser->parse($fileEnclosed), 'Test enclosed content is linked.');
     $testFile->delete();
     $fileShortcode = '[file_link,id="-1"]';
     $fileEnclosed = '[file_link,id="-1"]Example Content[/file_link]';
     $this->assertEquals('', $parser->parse('[file_link]'), 'Test that invalid ID attributes are not parsed.');
     $this->assertEquals('', $parser->parse('[file_link,id="text"]'));
     $this->assertEquals('', $parser->parse('[file_link]Example Content[/file_link]'));
     if (class_exists('ErrorPage')) {
         $errorPage = ErrorPage::get()->filter('ErrorCode', 404)->First();
         $this->assertEquals($errorPage->Link(), $parser->parse($fileShortcode), 'Test link to 404 page if no suitable matches.');
         $this->assertEquals(sprintf('<a href="%s">Example Content</a>', $errorPage->Link()), $parser->parse($fileEnclosed));
     } else {
         $this->assertEquals('', $parser->parse($fileShortcode), 'Short code is removed if file record is not present.');
         $this->assertEquals('', $parser->parse($fileEnclosed));
     }
 }
 public function testLinkShortcodeHandler()
 {
     $aboutPage = $this->objFromFixture('Page', 'about');
     $errorPage = $this->objFromFixture('ErrorPage', '404');
     $redirectPage = $this->objFromFixture('RedirectorPage', 'external');
     $parser = new ShortcodeParser();
     $parser->register('sitetree_link', array('SiteTree', 'link_shortcode_handler'));
     $aboutShortcode = sprintf('[sitetree_link,id=%d]', $aboutPage->ID);
     $aboutEnclosed = sprintf('[sitetree_link,id=%d]Example Content[/sitetree_link]', $aboutPage->ID);
     $aboutShortcodeExpected = $aboutPage->Link();
     $aboutEnclosedExpected = sprintf('<a href="%s">Example Content</a>', $aboutPage->Link());
     $this->assertEquals($aboutShortcodeExpected, $parser->parse($aboutShortcode), 'Test that simple linking works.');
     $this->assertEquals($aboutEnclosedExpected, $parser->parse($aboutEnclosed), 'Test enclosed content is linked.');
     $aboutPage->delete();
     $this->assertEquals($aboutShortcodeExpected, $parser->parse($aboutShortcode), 'Test that deleted pages still link.');
     $this->assertEquals($aboutEnclosedExpected, $parser->parse($aboutEnclosed));
     $aboutShortcode = '[sitetree_link,id="-1"]';
     $aboutEnclosed = '[sitetree_link,id="-1"]Example Content[/sitetree_link]';
     $aboutShortcodeExpected = $errorPage->Link();
     $aboutEnclosedExpected = sprintf('<a href="%s">Example Content</a>', $errorPage->Link());
     $this->assertEquals($aboutShortcodeExpected, $parser->parse($aboutShortcode), 'Test link to 404 page if no suitable matches.');
     $this->assertEquals($aboutEnclosedExpected, $parser->parse($aboutEnclosed));
     $redirectShortcode = sprintf('[sitetree_link,id=%d]', $redirectPage->ID);
     $redirectEnclosed = sprintf('[sitetree_link,id=%d]Example Content[/sitetree_link]', $redirectPage->ID);
     $redirectExpected = 'http://www.google.com?a&amp;b';
     $this->assertEquals($redirectExpected, $parser->parse($redirectShortcode));
     $this->assertEquals(sprintf('<a href="%s">Example Content</a>', $redirectExpected), $parser->parse($redirectEnclosed));
     $this->assertEquals('', $parser->parse('[sitetree_link]'), 'Test that invalid ID attributes are not parsed.');
     $this->assertEquals('', $parser->parse('[sitetree_link,id="text"]'));
     $this->assertEquals('', $parser->parse('[sitetree_link]Example Content[/sitetree_link]'));
 }
 public function testShortcodeHandlerAddsDefaultAttributes()
 {
     $image = $this->objFromFixture('Image', 'imageWithoutTitle');
     $parser = new ShortcodeParser();
     $parser->register('image', array('Image', 'handle_shortcode'));
     $this->assertEquals(sprintf('<img src="%s" alt="%s">', $image->Link(), $image->Title), $parser->parse(sprintf('[image id="%d"]', $image->ID)));
 }
 public function replaceChildrenCalls($html)
 {
     $codes = new ShortcodeParser();
     $codes->register('CHILDREN', array($this, 'includeChildren'));
     return $codes->parse($html);
 }