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 {
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
protected $arguments, $contents, $tagName, $parser;
|
2013-10-09 06:02:36 +02:00
|
|
|
protected $extra = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function setUp() {
|
|
|
|
ShortcodeParser::get('test')->register('test_shortcode', array($this, 'shortcodeSaver'));
|
|
|
|
$this->parser = ShortcodeParser::get('test');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
parent::setUp();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-06-30 16:59:55 +02:00
|
|
|
public function tearDown() {
|
|
|
|
ShortcodeParser::get('test')->unregister('test_shortcode');
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Tests that valid short codes that have not been registered are not replaced.
|
|
|
|
*/
|
|
|
|
public function testNotRegisteredShortcode() {
|
2013-02-14 03:11:10 +01:00
|
|
|
ShortcodeParser::$error_behavior = ShortcodeParser::STRIP;
|
2013-02-21 23:34:15 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'',
|
|
|
|
$this->parser->parse('[not_shortcode]')
|
|
|
|
);
|
|
|
|
|
2013-02-21 23:34:15 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'<img class="">',
|
|
|
|
$this->parser->parse('<img class="[not_shortcode]">')
|
|
|
|
);
|
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
ShortcodeParser::$error_behavior = ShortcodeParser::WARN;
|
2013-02-21 23:34:15 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'<strong class="warning">[not_shortcode]</strong>',
|
|
|
|
$this->parser->parse('[not_shortcode]')
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
ShortcodeParser::$error_behavior = ShortcodeParser::LEAVE;
|
2013-02-21 23:34:15 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('[not_shortcode]',
|
|
|
|
$this->parser->parse('[not_shortcode]'));
|
|
|
|
$this->assertEquals('[not_shortcode /]',
|
|
|
|
$this->parser->parse('[not_shortcode /]'));
|
|
|
|
$this->assertEquals('[not_shortcode,foo="bar"]',
|
|
|
|
$this->parser->parse('[not_shortcode,foo="bar"]'));
|
|
|
|
$this->assertEquals('[not_shortcode]a[/not_shortcode]',
|
|
|
|
$this->parser->parse('[not_shortcode]a[/not_shortcode]'));
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->assertEquals('[/not_shortcode]',
|
|
|
|
$this->parser->parse('[/not_shortcode]'));
|
2013-02-21 23:34:15 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'<img class="[not_shortcode]">',
|
|
|
|
$this->parser->parse('<img class="[not_shortcode]">')
|
|
|
|
);
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testSimpleTag() {
|
2013-02-14 03:11:10 +01:00
|
|
|
$tests = array(
|
2016-01-06 00:34:58 +01:00
|
|
|
'[test_shortcode]',
|
2013-02-14 03:11:10 +01:00
|
|
|
'[test_shortcode ]', '[test_shortcode,]', '[test_shortcode, ]'.
|
|
|
|
'[test_shortcode/]', '[test_shortcode /]', '[test_shortcode,/]', '[test_shortcode, /]'
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
foreach($tests as $test) {
|
|
|
|
$this->parser->parse($test);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
$this->assertEquals(array(), $this->arguments, $test);
|
|
|
|
$this->assertEquals('', $this->contents, $test);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName, $test);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testOneArgument() {
|
|
|
|
$tests = array (
|
2013-02-14 03:11:10 +01:00
|
|
|
'[test_shortcode foo="bar"]', '[test_shortcode,foo="bar"]',
|
|
|
|
"[test_shortcode foo='bar']", "[test_shortcode,foo='bar']",
|
|
|
|
'[test_shortcode foo = "bar" /]', '[test_shortcode, foo = "bar" /]'
|
2009-10-11 02:06:57 +02:00
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
foreach($tests as $test) {
|
|
|
|
$this->parser->parse($test);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
$this->assertEquals(array('foo' => 'bar'), $this->arguments, $test);
|
|
|
|
$this->assertEquals('', $this->contents, $test);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName, $test);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testMultipleArguments() {
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->parser->parse('[test_shortcode foo = "bar",bar=\'foo\', baz="buz"]');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
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);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testEnclosing() {
|
|
|
|
$this->parser->parse('[test_shortcode]foo[/test_shortcode]');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
$this->assertEquals(array(), $this->arguments);
|
|
|
|
$this->assertEquals('foo', $this->contents);
|
|
|
|
$this->assertEquals('test_shortcode', $this->tagName);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
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]');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
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);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testShortcodeEscaping() {
|
2013-02-21 22:30:51 +01:00
|
|
|
$this->assertEquals(
|
2016-01-06 00:34:58 +01:00
|
|
|
'[test_shortcode]',
|
2013-02-21 22:30:51 +01:00
|
|
|
$this->parser->parse('[[test_shortcode]]')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'[test_shortcode /]',
|
|
|
|
$this->parser->parse('[[test_shortcode /]]')
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'[test_shortcode]content[/test_shortcode]',
|
|
|
|
$this->parser->parse('[[test_shortcode]content[/test_shortcode]]'
|
|
|
|
));
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'[test_shortcode]content',
|
|
|
|
$this->parser->parse('[[test_shortcode]][test_shortcode]content[/test_shortcode]')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'[test_shortcode]content[/test_shortcode]content2',
|
|
|
|
$this->parser->parse('[[test_shortcode]content[/test_shortcode]][test_shortcode]content2[/test_shortcode]'
|
|
|
|
));
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'[[Doesnt strip double [ character if not a shortcode',
|
|
|
|
$this->parser->parse('[[Doesnt strip double [ character if not a [test_shortcode]shortcode[/test_shortcode]'
|
|
|
|
));
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-21 22:30:51 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'[[Doesnt shortcode get confused by double ]] characters',
|
2013-08-21 08:54:05 +02:00
|
|
|
$this->parser->parse(
|
|
|
|
'[[Doesnt [test_shortcode]shortcode[/test_shortcode] get confused by double ]] characters')
|
|
|
|
);
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2013-02-14 03:11:10 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
public function testUnquotedArguments() {
|
2013-04-26 01:00:13 +02:00
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode,foo=bar!,baz = buz123]'));
|
|
|
|
$this->assertEquals(array('foo' => 'bar!', 'baz' => 'buz123'), $this->arguments);
|
2009-10-11 02:06:57 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-03-09 03:00:03 +01:00
|
|
|
public function testSpacesForDelimiter() {
|
2013-04-26 01:00:13 +02:00
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode foo=bar! baz = buz123]'));
|
|
|
|
$this->assertEquals(array('foo' => 'bar!', 'baz' => 'buz123'), $this->arguments);
|
2012-03-09 03:00:03 +01:00
|
|
|
}
|
|
|
|
|
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.'
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
$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]'));
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
protected function assertEqualsIgnoringWhitespace($a, $b, $message = null) {
|
|
|
|
$this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
public function testtExtract() {
|
|
|
|
// Left extracts to before the current block
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
2016-01-06 00:34:58 +01:00
|
|
|
'Code<div>FooBar</div>',
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->parser->parse('<div>Foo[test_shortcode class=left]Code[/test_shortcode]Bar</div>')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Even if the immediate parent isn't a the current block
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'Code<div>Foo<b>BarBaz</b>Qux</div>',
|
|
|
|
$this->parser->parse('<div>Foo<b>Bar[test_shortcode class=left]Code[/test_shortcode]Baz</b>Qux</div>')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Center splits the current block
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
2016-01-06 00:34:58 +01:00
|
|
|
'<div>Foo</div>Code<div>Bar</div>',
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->parser->parse('<div>Foo[test_shortcode class=center]Code[/test_shortcode]Bar</div>')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Even if the immediate parent isn't a the current block
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
2016-01-06 00:34:58 +01:00
|
|
|
'<div>Foo<b>Bar</b></div>Code<div><b>Baz</b>Qux</div>',
|
2013-02-14 03:11:10 +01:00
|
|
|
$this->parser->parse('<div>Foo<b>Bar[test_shortcode class=center]Code[/test_shortcode]Baz</b>Qux</div>')
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-02-14 03:11:10 +01:00
|
|
|
// No class means don't extract
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'<div>FooCodeBar</div>',
|
|
|
|
$this->parser->parse('<div>Foo[test_shortcode]Code[/test_shortcode]Bar</div>')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:44:00 +02:00
|
|
|
public function testShortcodesInsideScriptTag() {
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'<script>hello</script>',
|
|
|
|
$this->parser->parse('<script>[test_shortcode]hello[/test_shortcode]</script>')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:39:52 +02:00
|
|
|
public function testFalseyArguments() {
|
|
|
|
$this->parser->parse('<p>[test_shortcode falsey=0]');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'falsey' => '',
|
|
|
|
), $this->arguments);
|
|
|
|
}
|
|
|
|
|
2015-06-22 10:44:00 +02:00
|
|
|
public function testNumericShortcodes() {
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'[2]',
|
|
|
|
$this->parser->parse('[2]')
|
|
|
|
);
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'<script>[2]</script>',
|
|
|
|
$this->parser->parse('<script>[2]</script>')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->parser->register('2', function($attributes, $content, $this, $tag, $extra) {
|
|
|
|
return 'this is 2';
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'this is 2',
|
|
|
|
$this->parser->parse('[2]')
|
|
|
|
);
|
|
|
|
$this->assertEqualsIgnoringWhitespace(
|
|
|
|
'<script>this is 2</script>',
|
|
|
|
$this->parser->parse('<script>[2]</script>')
|
|
|
|
);
|
2016-06-30 16:59:55 +02:00
|
|
|
|
|
|
|
$this->parser->unregister('2');
|
2015-06-22 10:44:00 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 06:02:36 +02:00
|
|
|
public function testExtraContext() {
|
|
|
|
$this->parser->parse('<a href="[test_shortcode]">Test</a>');
|
|
|
|
|
|
|
|
$this->assertInstanceOf('DOMNode', $this->extra['node']);
|
|
|
|
$this->assertInstanceOf('DOMElement', $this->extra['element']);
|
|
|
|
$this->assertEquals($this->extra['element']->tagName, 'a');
|
|
|
|
}
|
|
|
|
|
2016-06-30 16:59:55 +02:00
|
|
|
public function testNoParseAttemptIfNoCode() {
|
|
|
|
$stub = $this->getMock('ShortcodeParser', array('replaceElementTagsWithMarkers'));
|
|
|
|
$stub->register('test', function() {
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
|
|
|
|
$stub->expects($this->never())
|
|
|
|
->method('replaceElementTagsWithMarkers')->will($this->returnValue(array('', '')));
|
|
|
|
|
|
|
|
$stub->parse('<p>test</p>');
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
// -----------------------------------------------------------------------------------------------------------------
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
/**
|
|
|
|
* Stores the result of a shortcode parse in object properties for easy testing access.
|
|
|
|
*/
|
2013-10-09 06:02:36 +02:00
|
|
|
public function shortcodeSaver($arguments, $content, $parser, $tagName, $extra) {
|
2009-10-11 02:06:57 +02:00
|
|
|
$this->arguments = $arguments;
|
2013-10-09 06:02:36 +02:00
|
|
|
$this->contents = $content;
|
|
|
|
$this->tagName = $tagName;
|
|
|
|
$this->extra = $extra;
|
|
|
|
|
2009-10-11 02:06:57 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-03-09 02:25:42 +01:00
|
|
|
}
|