2009-10-11 02:06:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-10-11 02:06:57 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ShortcodeParserTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $arguments, $contents, $tagName, $parser;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
ShortcodeParser::get('test')->register('test_shortcode', array($this, 'shortcodeSaver'));
|
|
|
|
$this->parser = ShortcodeParser::get('test');
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that valid short codes that have not been registered are not replaced.
|
|
|
|
*/
|
|
|
|
public function testNotRegisteredShortcode() {
|
|
|
|
$this->assertEquals('[not_shortcode]', $this->parser->parse('[not_shortcode]'));
|
|
|
|
$this->assertEquals('[not_shortcode /]', $this->parser->parse('[not_shortcode /]'));
|
2012-03-09 03:00:03 +01:00
|
|
|
$this->assertEquals('[not_shortcode,foo="bar"]', $this->parser->parse('[not_shortcode,foo="bar"]'));
|
2009-10-11 02:06:57 +02:00
|
|
|
$this->assertEquals('[not_shortcode]a[/not_shortcode]', $this->parser->parse('[not_shortcode]a[/not_shortcode]'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleTag() {
|
2012-03-09 03:00:03 +01:00
|
|
|
$tests = array('[test_shortcode]', '[test_shortcode ]', '[test_shortcode,]', '[test_shortcode/]', '[test_shortcode /]');
|
2009-10-11 02:06:57 +02:00
|
|
|
|
|
|
|
foreach($tests as $test) {
|
|
|
|
$this->parser->parse($test);
|
|
|
|
|
|
|
|
$this->assertEquals(array(), $this->arguments, $test);
|
|
|
|
$this->assertEquals('', $this->contents, $test);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName, $test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOneArgument() {
|
|
|
|
$tests = array (
|
2012-03-09 03:00:03 +01:00
|
|
|
'[test_shortcode,foo="bar"]',
|
|
|
|
"[test_shortcode,foo='bar']",
|
|
|
|
'[test_shortcode,foo = "bar" /]'
|
2009-10-11 02:06:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach($tests as $test) {
|
|
|
|
$this->parser->parse($test);
|
|
|
|
|
|
|
|
$this->assertEquals(array('foo' => 'bar'), $this->arguments, $test);
|
|
|
|
$this->assertEquals('', $this->contents, $test);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName, $test);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleArguments() {
|
2012-03-09 03:00:03 +01:00
|
|
|
$this->parser->parse('[test_shortcode,foo = "bar",bar=\'foo\',baz="buz"]');
|
2009-10-11 02:06:57 +02:00
|
|
|
|
|
|
|
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', 'baz' => 'buz'), $this->arguments);
|
|
|
|
$this->assertEquals('', $this->contents);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnclosing() {
|
|
|
|
$this->parser->parse('[test_shortcode]foo[/test_shortcode]');
|
|
|
|
|
|
|
|
$this->assertEquals(array(), $this->arguments);
|
|
|
|
$this->assertEquals('foo', $this->contents);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnclosingWithArguments() {
|
2012-03-09 03:00:03 +01:00
|
|
|
$this->parser->parse('[test_shortcode,foo = "bar",bar=\'foo\',baz="buz"]foo[/test_shortcode]');
|
2009-10-11 02:06:57 +02:00
|
|
|
|
|
|
|
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', 'baz' => 'buz'), $this->arguments);
|
|
|
|
$this->assertEquals('foo', $this->contents);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShortcodeEscaping() {
|
|
|
|
$this->assertEquals('[test_shortcode]', $this->parser->parse('[[test_shortcode]]'));
|
|
|
|
$this->assertEquals('[test_shortcode]content[/test_shortcode]', $this->parser->parse('[[test_shortcode]content[/test_shortcode]]'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnquotedArguments() {
|
2012-03-09 02:25:42 +01:00
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode,foo=bar,baz = buz]'));
|
2009-10-11 02:06:57 +02:00
|
|
|
$this->assertEquals(array('foo' => 'bar', 'baz' => 'buz'), $this->arguments);
|
|
|
|
}
|
|
|
|
|
2012-03-09 03:00:03 +01:00
|
|
|
public function testSpacesForDelimiter() {
|
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode foo=bar baz = buz]'));
|
|
|
|
$this->assertEquals(array('foo' => 'bar', 'baz' => 'buz'), $this->arguments);
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testSelfClosingTag() {
|
|
|
|
$this->assertEquals (
|
|
|
|
'morecontent',
|
2012-03-09 02:25:42 +01:00
|
|
|
$this->parser->parse('[test_shortcode,id="1"/]more[test_shortcode,id="2"]content[/test_shortcode]'),
|
2009-10-11 02:06:57 +02:00
|
|
|
'Assert that self-closing tags are respected during parsing.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $this->arguments['id']);
|
|
|
|
}
|
2012-05-23 08:18:06 +02:00
|
|
|
|
|
|
|
public function testConsecutiveTags() {
|
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode][test_shortcode]'));
|
|
|
|
}
|
2009-10-11 02:06:57 +02:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the result of a shortcode parse in object properties for easy testing access.
|
|
|
|
*/
|
|
|
|
public function shortcodeSaver($arguments, $content = null, $parser, $tagName = null) {
|
|
|
|
$this->arguments = $arguments;
|
|
|
|
$this->contents = $content;
|
|
|
|
$this->tagName = $tagName;
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2012-03-09 02:25:42 +01:00
|
|
|
}
|