Move all composer files inside the common directory

- 2022년 3월 개발팀 결정사항 적용
- 모듈 등 서드파티 자료 개발시 composer를 사용하면 상위 경로에 있는 코어의
  composer.json을 수정하고, 코어의 vendor 디렉토리를 건드리는 것이 기본값임
- 이를 방지하기 위해 코어의 composer.json과 vendor를 common 디렉토리 안으로
  이동하여, 모듈 경로에서 상위 폴더로 인식하지 않도록 함
This commit is contained in:
Kijin Sung 2022-12-26 16:33:32 +09:00
parent 7b912d21fc
commit 5fff6b6eab
1478 changed files with 2 additions and 2 deletions

View file

@ -0,0 +1,141 @@
<?php
class CodeDefinitionBuilderTest extends PHPUnit_Framework_TestCase
{
/**
* @var CodeDefinitionBuilderStub
*/
private $_builder;
protected function setUp()
{
$this->_builder = new CodeDefinitionBuilderStub('foo', 'bar');
}
public function testConstructor()
{
$codeDefinition = $this->_builder->build();
$this->assertInstanceOf('JBBCode\CodeDefinition', $codeDefinition);
$this->assertEquals('foo', $codeDefinition->getTagName());
$this->assertEquals('bar', $codeDefinition->getReplacementText());
}
public function testSetTagName()
{
$this->assertSame($this->_builder, $this->_builder->setTagName('baz'));
$this->assertEquals('baz', $this->_builder->build()->getTagName());
}
public function testSetReplacementText()
{
$this->assertSame($this->_builder, $this->_builder->setReplacementText('baz'));
$this->assertEquals('baz', $this->_builder->build()->getReplacementText());
}
public function testSetUseOption()
{
$this->assertFalse($this->_builder->build()->usesOption());
$this->assertSame($this->_builder, $this->_builder->setUseOption(true));
$this->assertTrue($this->_builder->build()->usesOption());
}
public function testSetParseContent()
{
$this->assertTrue($this->_builder->build()->parseContent());
$this->assertSame($this->_builder, $this->_builder->setParseContent(false));
$this->assertFalse($this->_builder->build()->parseContent());
}
public function testSetNestLimit()
{
$this->assertEquals(-1, $this->_builder->build()->getNestLimit());
$this->assertSame($this->_builder, $this->_builder->setNestLimit(1));
$this->assertEquals(1, $this->_builder->build()->getNestLimit());
}
/**
* @expectedException InvalidArgumentException
* @dataProvider invalidNestLimitProvider
*/
public function testSetInvalidNestLimit($limit)
{
$this->_builder->setNestLimit($limit);
}
public function testSetOptionValidator()
{
$this->assertEmpty($this->_builder->getOptionValidators());
$urlValidator = new JBBCode\validators\UrlValidator();
$this->assertSame($this->_builder, $this->_builder->setOptionValidator($urlValidator));
$this->assertArrayHasKey('foo', $this->_builder->getOptionValidators());
$this->assertContains($urlValidator, $this->_builder->getOptionValidators());
$otherUrlValidator = new JBBCode\validators\UrlValidator();
$this->assertSame($this->_builder, $this->_builder->setOptionValidator($otherUrlValidator, 'url'));
$this->assertArrayHasKey('url', $this->_builder->getOptionValidators());
$this->assertContains($urlValidator, $this->_builder->getOptionValidators());
$this->assertContains($otherUrlValidator, $this->_builder->getOptionValidators());
}
public function testSetBodyValidator()
{
$this->assertNull($this->_builder->getBodyValidator());
$validator = new JBBCode\validators\UrlValidator();
$this->assertSame($this->_builder, $this->_builder->setBodyValidator($validator));
$this->assertSame($validator, $this->_builder->getBodyValidator());
}
/**
* @depends testSetOptionValidator
*/
public function testRemoveOptionValidator()
{
$this->assertSame($this->_builder, $this->_builder->removeOptionValidator());
$this->assertEmpty($this->_builder->getOptionValidators());
$this->_builder->setOptionValidator(new JBBCode\validators\UrlValidator());
$this->assertSame($this->_builder, $this->_builder->removeOptionValidator());
$this->assertEmpty($this->_builder->getOptionValidators());
}
/**
* @depends testSetBodyValidator
*/
public function testRemoveBodyValidator()
{
$this->assertSame($this->_builder, $this->_builder->removeBodyValidator());
$this->assertNull($this->_builder->getBodyValidator());
$this->_builder->setOptionValidator(new JBBCode\validators\UrlValidator());
$this->assertSame($this->_builder, $this->_builder->removeBodyValidator());
$this->assertNull($this->_builder->getBodyValidator());
}
public function invalidNestLimitProvider()
{
return array(
array(-2),
array(null),
array(false),
);
}
}
class CodeDefinitionBuilderStub extends \JBBCode\CodeDefinitionBuilder
{
/**
* @return \JBBCode\InputValidator
*/
public function getBodyValidator()
{
return $this->bodyValidator;
}
/**
* @return \JBBCode\InputValidator[]
*/
public function getOptionValidators()
{
return $this->optionValidator;
}
}

View file

@ -0,0 +1,37 @@
<?php
/**
* Test cases for the default bbcode set.
*
* @author jbowens
* @since May 2013
*/
class DefaultCodeDefinitionSetTest extends PHPUnit_Framework_TestCase
{
public function testGetCodeDefinitions()
{
$dcds = new JBBCode\DefaultCodeDefinitionSet();
$definitions = $dcds->getCodeDefinitions();
$this->assertInternalType('array', $definitions);
$parser = new JBBCode\Parser();
$this->assertFalse($parser->codeExists('b'));
$this->assertFalse($parser->codeExists('i'));
$this->assertFalse($parser->codeExists('u'));
$this->assertFalse($parser->codeExists('url', true));
$this->assertFalse($parser->codeExists('img'));
$this->assertFalse($parser->codeExists('img', true));
$this->assertFalse($parser->codeExists('color', true));
$parser->addCodeDefinitionSet($dcds);
$this->assertTrue($parser->codeExists('b'));
$this->assertTrue($parser->codeExists('i'));
$this->assertTrue($parser->codeExists('u'));
$this->assertTrue($parser->codeExists('url', true));
$this->assertTrue($parser->codeExists('img'));
$this->assertTrue($parser->codeExists('img', true));
$this->assertTrue($parser->codeExists('color', true));
}
}

View file

@ -0,0 +1,80 @@
<?php
class ElementNodeTest extends PHPUnit_Framework_TestCase
{
/** @var JBBCode\ElementNode */
private $_elementNode;
protected function setUp()
{
$this->_elementNode = new JBBCode\ElementNode();
}
public function testConstructor()
{
$this->assertNull($this->_elementNode->getCodeDefinition());
$this->assertEmpty($this->_elementNode->getTagName());
$this->assertEmpty($this->_elementNode->getAttribute());
$this->assertEmpty($this->_elementNode->getChildren());
$this->assertEmpty($this->_elementNode->getAsText());
$this->assertEmpty($this->_elementNode->getAsHTML());
}
public function testAccept()
{
$mock = $this->getMock('JBBCode\NodeVisitor',
array('visitDocumentElement', 'visitTextNode', 'visitElementNode'));
$mock->expects($this->never())
->method('visitDocumentElement');
$mock->expects($this->never())
->method('visitTextNode');
$mock->expects($this->once())
->method('visitElementNode')
->with($this->equalTo($this->_elementNode));
$this->_elementNode->accept($mock);
}
public function testSetCodeDefinition()
{
$mock = $this->getMock('JBBCode\CodeDefinition', array('getTagName'));
$mock->expects($this->once())
->method('getTagName')
->will($this->returnValue('foo'));
$this->_elementNode->setCodeDefinition($mock);
$this->assertSame($mock, $this->_elementNode->getCodeDefinition());
$this->assertEquals('foo', $this->_elementNode->getTagName());
}
public function testAddChild()
{
$mock = $this->getMock('JBBCode\ElementNode', array('setParent'));
$mock->expects($this->once())
->method('setParent')
->with($this->equalTo($this->_elementNode));
$this->_elementNode->addChild($mock);
$this->assertContains($mock, $this->_elementNode->getChildren());
}
public function testIsTextNode()
{
$this->assertFalse($this->_elementNode->isTextNode());
}
public function testGetAsBBCode()
{
$builder = new JBBCode\CodeDefinitionBuilder('foo', 'bar');
$codeDefinition = $builder->build();
$this->_elementNode->setCodeDefinition($codeDefinition);
$this->assertEquals('[foo][/foo]', $this->_elementNode->getAsBBCode());
$this->_elementNode->setAttribute(array('bar' => 'baz'));
$this->assertEquals('[foo bar=baz][/foo]', $this->_elementNode->getAsBBCode());
/** @ticket 55 */
$this->_elementNode->setAttribute(array(
'bar' => 'baz',
'foo' => 'bar'
));
$this->assertEquals('[foo=bar bar=baz][/foo]', $this->_elementNode->getAsBBCode());
}
}

View file

@ -0,0 +1,91 @@
<?php
/**
* Test cases for the code definition parameter that disallows parsing
* of an element's content.
*
* @author jbowens
*/
class ParseContentTest extends PHPUnit_Framework_TestCase
{
/**
* @var JBBCode\Parser
*/
private $_parser;
protected function setUp()
{
$this->_parser = new JBBCode\Parser();
$this->_parser->addCodeDefinitionSet(new JBBcode\DefaultCodeDefinitionSet());
}
/**
* Tests that when a bbcode is created with parseContent = false,
* its contents actually are not parsed.
*/
public function testSimpleNoParsing()
{
$this->_parser->addBBCode('verbatim', '{param}', false, false);
$this->_parser->parse('[verbatim]plain text[/verbatim]');
$this->assertEquals('plain text', $this->_parser->getAsHtml());
$this->_parser->parse('[verbatim][b]bold[/b][/verbatim]');
$this->assertEquals('[b]bold[/b]', $this->_parser->getAsHtml());
}
public function testNoParsingWithBufferText()
{
$this->_parser->addBBCode('verbatim', '{param}', false, false);
$this->_parser->parse('buffer text[verbatim]buffer text[b]bold[/b]buffer text[/verbatim]buffer text');
$this->assertEquals('buffer textbuffer text[b]bold[/b]buffer textbuffer text', $this->_parser->getAsHtml());
}
/**
* Tests that when a tag is not closed within an unparseable tag,
* the BBCode output does not automatically close that tag (because
* the contents were not parsed).
*/
public function testUnclosedTag()
{
$this->_parser->addBBCode('verbatim', '{param}', false, false);
$this->_parser->parse('[verbatim]i wonder [b]what will happen[/verbatim]');
$this->assertEquals('i wonder [b]what will happen', $this->_parser->getAsHtml());
$this->assertEquals('[verbatim]i wonder [b]what will happen[/verbatim]', $this->_parser->getAsBBCode());
}
/**
* Tests that an unclosed tag with parseContent = false ends cleanly.
*/
public function testUnclosedVerbatimTag()
{
$this->_parser->addBBCode('verbatim', '{param}', false, false);
$this->_parser->parse('[verbatim]yo this [b]text should not be bold[/b]');
$this->assertEquals('yo this [b]text should not be bold[/b]', $this->_parser->getAsHtml());
}
/**
* Tests a malformed closing tag for a verbatim block.
*/
public function testMalformedVerbatimClosingTag()
{
$this->_parser->addBBCode('verbatim', '{param}', false, false);
$this->_parser->parse('[verbatim]yo this [b]text should not be bold[/b][/verbatim');
$this->assertEquals('yo this [b]text should not be bold[/b][/verbatim', $this->_parser->getAsHtml());
}
/**
* Tests an immediate end after a verbatim.
*/
public function testVerbatimThenEof()
{
$parser = new JBBCode\Parser();
$parser->addBBCode('verbatim', '{param}', false, false);
$parser->parse('[verbatim]');
$this->assertEquals('', $parser->getAsHtml());
}
}

View file

@ -0,0 +1,150 @@
<?php
class ParserTest extends PHPUnit_Framework_TestCase
{
/**
* @var JBBCode\Parser
*/
private $_parser;
protected function setUp()
{
$this->_parser = new JBBCode\Parser();
$this->_parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
}
public function testAddCodeDefinition()
{
$parser = new JBBCode\Parser();
$this->assertFalse($parser->codeExists('foo', true));
$this->assertFalse($parser->codeExists('foo', false));
}
public function testAddBBCode()
{
$parser = new JBBCode\Parser();
$this->assertFalse($parser->codeExists('foo', true));
$this->assertFalse($parser->codeExists('foo', false));
$this->assertSame($parser, $parser->addBBCode('foo', 'bar', true));
$this->assertTrue($parser->codeExists('foo', true));
$this->assertFalse($parser->codeExists('foo', false));
$this->assertSame($parser, $parser->addBBCode('foo', 'bar', true));
$this->assertTrue($parser->codeExists('foo', true));
$this->assertFalse($parser->codeExists('foo', false));
$this->assertSame($parser, $parser->addBBCode('foo', 'bar', false));
$this->assertTrue($parser->codeExists('foo', true));
$this->assertTrue($parser->codeExists('foo', false));
}
/**
* Check for empty strings being the result of empty input
*/
public function testParseEmptyString()
{
$parser = $this->_parser->parse('');
$this->assertEmpty($parser->getAsBBCode());
$this->assertEmpty($parser->getAsText());
$this->assertEmpty($parser->getAsHTML());
}
/**
* Test for artifacts of previous parses
*/
public function testParseContentCleared()
{
$parser = $this->_parser->parse('foo');
$this->assertEquals('foo', $parser->getAsText());
$this->assertEquals('foo', $parser->getAsHTML());
$this->assertEquals('foo', $parser->getAsBBCode());
$parser->parse('bar');
$this->assertEquals('bar', $parser->getAsText());
$this->assertEquals('bar', $parser->getAsHTML());
$this->assertEquals('bar', $parser->getAsBBCode());
}
/**
* @param string $code
* @param string[] $expected
* @dataProvider textCodeProvider
*/
public function testParse($code, $expected)
{
$parser = $this->_parser->parse($code);
$this->assertEquals($expected['text'], $parser->getAsText());
$this->assertEquals($expected['html'], $parser->getAsHTML());
$this->assertEquals($expected['bbcode'], $parser->getAsBBCode());
}
public function textCodeProvider()
{
return array(
array(
'foo',
array(
'text' => 'foo',
'html' => 'foo',
'bbcode' => 'foo',
)
),
array(
'[b]this is bold[/b]',
array(
'text' => 'this is bold',
'html' => '<strong>this is bold</strong>',
'bbcode' => '[b]this is bold[/b]',
)
),
array(
'[b]this is bold',
array(
'text' => 'this is bold',
'html' => '<strong>this is bold</strong>',
'bbcode' => '[b]this is bold[/b]',
)
),
array(
'buffer text [b]this is bold[/b] buffer text',
array(
'text' => 'buffer text this is bold buffer text',
'html' => 'buffer text <strong>this is bold</strong> buffer text',
'bbcode' => 'buffer text [b]this is bold[/b] buffer text',
)
),
array(
'this is some text with [b]bold tags[/b] and [i]italics[/i] and things like [u]that[/u].',
array(
'text' => 'this is some text with bold tags and italics and things like that.',
'html' => 'this is some text with <strong>bold tags</strong> and <em>italics</em> and things like <u>that</u>.',
'bbcode' => 'this is some text with [b]bold tags[/b] and [i]italics[/i] and things like [u]that[/u].',
)
),
array(
'This contains a [url=http://jbbcode.com]url[/url] which uses an option.',
array(
'text' => 'This contains a url which uses an option.',
'html' => 'This contains a <a href="http://jbbcode.com">url</a> which uses an option.',
'bbcode' => 'This contains a [url=http://jbbcode.com]url[/url] which uses an option.',
)
),
array(
'This doesn\'t use the url option [url]http://jbbcode.com[/url].',
array(
'text' => 'This doesn\'t use the url option http://jbbcode.com.',
'html' => 'This doesn\'t use the url option <a href="http://jbbcode.com">http://jbbcode.com</a>.',
'bbcode' => 'This doesn\'t use the url option [url]http://jbbcode.com[/url].',
)
),
);
}
}

View file

@ -0,0 +1,127 @@
<?php
/**
* A series of test cases for various potential parsing edge cases. This
* includes a lot of tests using brackets for things besides genuine tag
* names.
*
* @author jbowens
*
*/
class ParsingEdgeCaseTest extends PHPUnit_Framework_TestCase
{
/**
* A utility method for these tests that will evaluate
* its arguments as bbcode with a fresh parser loaded
* with only the default bbcodes. It returns the
* html output.
*/
private function defaultParse($bbcode)
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse($bbcode);
return $parser->getAsHtml();
}
/**
* Asserts that the given bbcode matches the given html when
* the bbcode is run through defaultParse.
*/
private function assertProduces($bbcode, $html)
{
$this->assertEquals($html, $this->defaultParse($bbcode));
}
/**
* Tests attempting to use a code that doesn't exist.
*/
public function testNonexistentCodeMalformed()
{
$this->assertProduces('[wat]', '[wat]');
}
/**
* Tests attempting to use a code that doesn't exist, but this
* time in a well-formed fashion.
*
* @depends testNonexistentCodeMalformed
*/
public function testNonexistentCodeWellformed()
{
$this->assertProduces('[wat]something[/wat]', '[wat]something[/wat]');
}
/**
* Tests a whole bunch of meaningless left brackets.
*/
public function testAllLeftBrackets()
{
$this->assertProduces('[[[[[[[[', '[[[[[[[[');
}
/**
* Tests a whole bunch of meaningless right brackets.
*/
public function testAllRightBrackets()
{
$this->assertProduces(']]]]]', ']]]]]');
}
/**
* Intermixes well-formed, meaningful tags with meaningless brackets.
*/
public function testRandomBracketsInWellformedCode()
{
$this->assertProduces('[b][[][[i]heh[/i][/b]',
'<strong>[[][<em>heh</em></strong>');
}
/**
* Tests an unclosed tag within a closed tag.
*/
public function testUnclosedWithinClosed()
{
$this->assertProduces('[url=http://jbbcode.com][b]oh yeah[/url]',
'<a href="http://jbbcode.com"><strong>oh yeah</strong></a>');
}
/**
* Tests half completed opening tag.
*/
public function testHalfOpenTag()
{
$this->assertProduces('[b', '[b');
$this->assertProduces('wut [url=http://jbbcode.com',
'wut [url=http://jbbcode.com');
}
/**
* Tests half completed closing tag.
*/
public function testHalfClosingTag()
{
$this->assertProduces('[b]this should be bold[/b',
'<strong>this should be bold[/b</strong>');
}
/**
* Tests lots of left brackets before the actual tag. For example:
* [[[[[[[[b]bold![/b]
*/
public function testLeftBracketsThenTag()
{
$this->assertProduces('[[[[[b]bold![/b]',
'[[[[<strong>bold!</strong>');
}
/**
* Tests a whitespace after left bracket.
*/
public function testWhitespaceAfterLeftBracketWhithoutTag()
{
$this->assertProduces('[ ABC ] ',
'[ ABC ] ');
}
}

View file

@ -0,0 +1,81 @@
<?php
class SimpleEvaluationTest extends PHPUnit_Framework_TestCase
{
/**
* A utility method for these tests that will evaluate
* its arguments as bbcode with a fresh parser loaded
* with only the default bbcodes. It returns the
* html output.
*/
private function defaultParse($bbcode)
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse($bbcode);
return $parser->getAsHtml();
}
/**
* Asserts that the given bbcode matches the given html when
* the bbcode is run through defaultParse.
*/
private function assertProduces($bbcode, $html)
{
$this->assertEquals($html, $this->defaultParse($bbcode));
}
public function testCodeOptions()
{
$code = 'This contains a [url=http://jbbcode.com/?b=2]url[/url] which uses an option.';
$html = 'This contains a <a href="http://jbbcode.com/?b=2">url</a> which uses an option.';
$this->assertProduces($code, $html);
}
public function testAttributes()
{
$parser = new JBBCode\Parser();
$builder = new JBBCode\CodeDefinitionBuilder('img', '<img src="{param}" height="{height}" alt="{alt}" />');
$parser->addCodeDefinition($builder->setUseOption(true)->setParseContent(false)->build());
$expected = 'Multiple <img src="http://jbbcode.com/img.png" height="50" alt="alt text" /> options.';
$code = 'Multiple [img height="50" alt="alt text"]http://jbbcode.com/img.png[/img] options.';
$parser->parse($code);
$result = $parser->getAsHTML();
$this->assertEquals($expected, $result);
$code = 'Multiple [img height=50 alt="alt text"]http://jbbcode.com/img.png[/img] options.';
$parser->parse($code);
$result = $parser->getAsHTML();
$this->assertEquals($expected, $result);
}
public function testNestingTags()
{
$code = '[url=http://jbbcode.com][b]hello [u]world[/u][/b][/url]';
$html = '<a href="http://jbbcode.com"><strong>hello <u>world</u></strong></a>';
$this->assertProduces($code, $html);
}
public function testBracketInTag()
{
$this->assertProduces('[b]:-[[/b]', '<strong>:-[</strong>');
}
public function testBracketWithSpaceInTag()
{
$this->assertProduces('[b]:-[ [/b]', '<strong>:-[ </strong>');
}
public function testBracketWithTextInTag()
{
$this->assertProduces('[b]:-[ foobar[/b]', '<strong>:-[ foobar</strong>');
}
public function testMultibleBracketsWithTextInTag()
{
$this->assertProduces('[b]:-[ [fo[o[bar[/b]', '<strong>:-[ [fo[o[bar</strong>');
}
}

View file

@ -0,0 +1,31 @@
<?php
class TextNodeTest extends PHPUnit_Framework_TestCase
{
/** @var JBBCode\TextNode */
private $_textNode;
protected function setUp()
{
$this->_textNode = new JBBCode\TextNode('');
}
public function accept()
{
$mock = $this->getMock('JBBCode\NodeVisitor',
array('visitDocumentElement', 'visitTextNode', 'visitElementNode'));
$mock->expects($this->never())
->method('visitDocumentElement');
$mock->expects($this->once())
->method('visitTextNode')
->with($this->equalTo($this->_textNode));
$mock->expects($this->never())
->method('visitElementNode');
$this->_textNode->accept($mock);
}
public function testIsTextNode()
{
$this->assertTrue($this->_textNode->isTextNode());
}
}

View file

@ -0,0 +1,116 @@
<?php
/**
* Test cases testing the functionality of the Tokenizer. The tokenizer
* is used by the parser to make parsing simpler.
*
* @author jbowens
*/
class TokenizerTest extends PHPUnit_Framework_TestCase
{
public function testEmptyString()
{
$tokenizer = new JBBCode\Tokenizer('');
$this->assertFalse($tokenizer->hasNext());
$this->assertNull($tokenizer->current());
$this->assertNull($tokenizer->next());
$this->assertEmpty($tokenizer->toString());
}
public function testHasNext()
{
$tokenizer = new JBBCode\Tokenizer('');
$this->assertFalse($tokenizer->hasNext());
$tokenizer = new JBBCode\Tokenizer('[');
$this->assertTrue($tokenizer->hasNext());
$tokenizer->next();
$this->assertFalse($tokenizer->hasNext());
}
public function testNext()
{
$tokenizer = new JBBCode\Tokenizer('[');
$this->assertEquals('[', $tokenizer->next());
$this->assertNull($tokenizer->next());
}
public function testCurrent()
{
$tokenizer = new JBBCode\Tokenizer('[');
$this->assertNull($tokenizer->current());
$tokenizer->next();
$this->assertEquals('[', $tokenizer->current());
}
public function testStepBack()
{
$tokenizer = new JBBCode\Tokenizer('');
$tokenizer->stepBack();
$this->assertFalse($tokenizer->hasNext());
$tokenizer = new JBBCode\Tokenizer('[');
$this->assertTrue($tokenizer->hasNext());
$this->assertEquals('[', $tokenizer->next());
$this->assertFalse($tokenizer->hasNext());
$tokenizer->stepBack();
$this->assertTrue($tokenizer->hasNext());
$this->assertEquals('[', $tokenizer->next());
}
public function testRestart()
{
$tokenizer = new JBBCode\Tokenizer('');
$tokenizer->restart();
$this->assertFalse($tokenizer->hasNext());
$tokenizer = new JBBCode\Tokenizer('[');
$tokenizer->next();
$tokenizer->restart();
$this->assertTrue($tokenizer->hasNext());
}
public function testToString()
{
$tokenizer = new JBBCode\Tokenizer('[');
$this->assertEquals('[', $tokenizer->toString());
$tokenizer->next();
$this->assertEmpty($tokenizer->toString());
}
/**
* @param string[] $tokens
* @dataProvider tokenProvider()
*/
public function testTokenize($tokens)
{
$string = implode('', $tokens);
$tokenizer = new JBBCode\Tokenizer($string);
$this->assertEquals($string, $tokenizer->toString());
$this->assertTrue($tokenizer->hasNext());
$this->assertNull($tokenizer->current());
foreach ($tokens as $token) {
$this->assertEquals($token, $tokenizer->next());
}
$this->assertNull($tokenizer->next());
$this->assertFalse($tokenizer->hasNext());
}
public function tokenProvider()
{
return array(
array(
array('foo'),
),
array(
array('foo', '[', 'b', ']', 'bar'),
),
array(
array('[', 'foo', ']'),
),
);
}
}

View file

@ -0,0 +1,2 @@
<?php
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Parser.php';

View file

@ -0,0 +1,37 @@
<?php
class CssColorValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* @var JBBCode\validators\CssColorValidator
*/
private $_validator;
protected function setUp()
{
$this->_validator = new JBBCode\validators\CssColorValidator();
}
/**
* @param string $color
* @dataProvider validColorProvider
*/
public function testValidColors($color)
{
$this->assertTrue($this->_validator->validate($color));
}
public function validColorProvider()
{
return array(
array('red'),
array('yellow'),
array('LightGoldenRodYellow'),
array('#000'),
array('#00ff00'),
array('rgba(255, 0, 0, 0.5)'),
array('rgba(50, 50, 50, 0.0)'),
);
}
}

View file

@ -0,0 +1,30 @@
<?php
class FnValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* Test custom functional validator implementations.
*
* @param JBBCode\validators\FnValidator $validator
* @dataProvider validatorProvider
*/
public function testValidator($validator)
{
$this->assertTrue($validator->validate('1234567890'));
$this->assertFalse($validator->validate('QWERTZUIOP'));
}
/**
* Provide custom numeric string validator implementations.
*
*/
public function validatorProvider()
{
return array(
array(new JBBCode\validators\FnValidator('is_numeric')),
array(new JBBCode\validators\FnValidator(function ($input) {
return is_numeric($input);
})),
);
}
}

View file

@ -0,0 +1,50 @@
<?php
class UrlValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* @var JBBCode\validators\UrlValidator
*/
private $_validator;
protected function setUp()
{
$this->_validator = new JBBCode\validators\UrlValidator();
}
/**
* @param string $url
* @dataProvider invalidUrlProvider
*/
public function testInvalidUrl($url)
{
$this->assertFalse($this->_validator->validate($url));
}
/**
* @param string $url
* @dataProvider validUrlProvider
*/
public function testValidUrl($url)
{
$this->assertTrue($this->_validator->validate($url));
}
public function invalidUrlProvider()
{
return array(
array('#yolo#swag'),
array('giehtiehwtaw352353%3'),
);
}
public function validUrlProvider()
{
return array(
array('http://google.com'),
array('http://jbbcode.com/docs'),
array('https://www.maps.google.com'),
);
}
}

View file

@ -0,0 +1,72 @@
<?php
/**
* Test cases for InputValidators.
*
* @author jbowens
* @since May 2013
*/
class ValidatorTest extends PHPUnit_Framework_TestCase
{
/**
* Tests an invalid url as an option to a url bbcode.
*
*/
public function testInvalidOptionUrlBBCode()
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse('[url=javascript:alert("HACKED!");]click me[/url]');
$this->assertEquals('[url=javascript:alert("HACKED!");]click me[/url]',
$parser->getAsHtml());
}
/**
* Tests an invalid url as the body to a url bbcode.
*
*/
public function testInvalidBodyUrlBBCode()
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse('[url]javascript:alert("HACKED!");[/url]');
$this->assertEquals('[url]javascript:alert("HACKED!");[/url]', $parser->getAsHtml());
}
/**
* Tests a valid url as the body to a url bbcode.
*
*/
public function testValidUrlBBCode()
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse('[url]http://jbbcode.com[/url]');
$this->assertEquals('<a href="http://jbbcode.com">http://jbbcode.com</a>',
$parser->getAsHtml());
}
/**
* Tests invalid CSS color values on the CssColorValidator.
*/
public function testInvalidCssColor()
{
$colorValidator = new JBBCode\validators\CssColorValidator();
$this->assertFalse($colorValidator->validate('" onclick="javascript: alert(\"gotcha!\");'));
$this->assertFalse($colorValidator->validate('"><marquee scrollamount="100'));
}
/**
* Tests invalid css colors in a color bbcode.
*
* @depends testInvalidCssColor
*/
public function testInvalidColorBBCode()
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse('[color=" onclick="alert(\'hey ya!\');]click me[/color]');
$this->assertEquals('[color=" onclick="alert(\'hey ya!\');]click me[/color]',
$parser->getAsHtml());
}
}

View file

@ -0,0 +1,76 @@
<?php
require_once dirname(dirname(__DIR__)) . '/visitors/HTMLSafeVisitor.php';
/**
* Test cases testing the HTMLSafe visitor, which escapes all html characters in the source text
*
* @author astax-t
*/
class HTMLSafeVisitorTest extends PHPUnit_Framework_TestCase
{
/**
* Asserts that the given bbcode string produces the given html string
* when parsed with the default bbcodes.
*/
public function assertProduces($bbcode, $html)
{
$parser = new \JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->parse($bbcode);
$htmlsafer = new JBBCode\visitors\HTMLSafeVisitor();
$parser->accept($htmlsafer);
$this->assertEquals($html, $parser->getAsHtml());
}
/**
* Tests escaping quotes and ampersands in simple text
*/
public function testQuoteAndAmp()
{
$this->assertProduces('te"xt te&xt', 'te&quot;xt te&amp;xt');
}
/**
* Tests escaping quotes and ampersands inside a BBCode tag
*/
public function testQuoteAndAmpInTag()
{
$this->assertProduces('[b]te"xt te&xt[/b]', '<strong>te&quot;xt te&amp;xt</strong>');
}
/**
* Tests escaping HTML tags
*/
public function testHtmlTag()
{
$this->assertProduces('<b>not bold</b>', '&lt;b&gt;not bold&lt;/b&gt;');
$this->assertProduces('[b]<b>bold</b>[/b] <hr>', '<strong>&lt;b&gt;bold&lt;/b&gt;</strong> &lt;hr&gt;');
}
/**
* Tests escaping ampersands in URL using [url]...[/url]
*/
public function testUrlParam()
{
$this->assertProduces('text [url]http://example.com/?a=b&c=d[/url] more text', 'text <a href="http://example.com/?a=b&amp;c=d">http://example.com/?a=b&amp;c=d</a> more text');
}
/**
* Tests escaping ampersands in URL using [url=...] tag
*/
public function testUrlOption()
{
$this->assertProduces('text [url=http://example.com/?a=b&c=d]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&amp;c=d">this is a &quot;link&quot;</a>');
}
/**
* Tests escaping ampersands in URL using [url=...] tag when URL is in quotes
*/
public function testUrlOptionQuotes()
{
$this->assertProduces('text [url="http://example.com/?a=b&c=d"]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&amp;c=d">this is a &quot;link&quot;</a>');
}
}

View file

@ -0,0 +1,80 @@
<?php
/**
* Test cases for CodeDefinition nest limits. If an element is nested beyond
* its CodeDefinition's nest limit, it should be removed from the parse tree.
*
* @author jbowens
* @since May 2013
*/
class NestLimitVisitorTest extends PHPUnit_Framework_TestCase
{
/** @var \JBBCode\visitors\NestLimitVisitor */
private $_nestLimitVisitor;
protected function setUp()
{
$this->_nestLimitVisitor = new \JBBCode\visitors\NestLimitVisitor();
}
public function testVisitDocumentElement()
{
$childMock = $this->getMock('JBBCode\ElementNode', array('accept'));
$childMock->expects($this->once())
->method('accept')
->with($this->equalTo($this->_nestLimitVisitor));
$mock = $this->getMock('JBBCode\DocumentElement', array('getChildren'));
$mock->expects($this->once())
->method('getChildren')
->will($this->returnValue(array(
$childMock
)));
$this->_nestLimitVisitor->visitDocumentElement($mock);
}
public function testVisitTextNode()
{
$mock = $this->getMockBuilder('JBBCode\TextNode')
->setMethods(array('accept'))
->disableOriginalConstructor()
->getMock();
$mock->expects($this->never())
->method('accept');
$this->_nestLimitVisitor->visitTextNode($mock);
}
/**
* Tests that when elements have no nest limits they may be
* nested indefinitely.
*/
public function testIndefiniteNesting()
{
$parser = new JBBCode\Parser();
$parser->addBBCode('b', '<strong>{param}</strong>', false, true, -1);
$parser->parse('[b][b][b][b][b][b][b][b]bold text[/b][/b][/b][/b][/b][/b][/b][/b]');
$this->assertEquals('<strong><strong><strong><strong><strong><strong><strong><strong>' .
'bold text' .
'</strong></strong></strong></strong></strong></strong></strong></strong>',
$parser->getAsHtml());
}
/**
* Test over nesting.
*/
public function testOverNesting()
{
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
$parser->addBBCode('quote', '<blockquote>{param}</blockquote>', false, true, 2);
$bbcode = '[quote][quote][quote]wut[/quote] huh?[/quote] i don\'t know[/quote]';
$parser->parse($bbcode);
$expectedBbcode = '[quote][quote] huh?[/quote] i don\'t know[/quote]';
$expectedHtml = '<blockquote><blockquote> huh?</blockquote> i don\'t know</blockquote>';
$this->assertEquals($expectedBbcode, $parser->getAsBBCode());
$this->assertEquals($expectedHtml, $parser->getAsHtml());
}
}

View file

@ -0,0 +1,91 @@
<?php
require_once dirname(dirname(__DIR__)) . '/visitors/SmileyVisitor.php';
class SmileyVisitorTest extends PHPUnit_Framework_TestCase
{
/** @var \JBBCode\visitors\SmileyVisitor */
private $_smileyVisitor;
protected function setUp()
{
$this->_smileyVisitor = new \JBBCode\visitors\SmileyVisitor();
}
public function testVisitDocumentElement()
{
$childMock = $this->getMock('JBBCode\ElementNode', array('accept'));
$childMock->expects($this->once())
->method('accept')
->with($this->equalTo($this->_smileyVisitor));
$mock = $this->getMock('JBBCode\DocumentElement', array('getChildren'));
$mock->expects($this->once())
->method('getChildren')
->will($this->returnValue(array(
$childMock,
)));
$this->_smileyVisitor->visitDocumentElement($mock);
}
public function testVisitElementNode()
{
$builder = new \JBBCode\CodeDefinitionBuilder('foo', 'bar');
$builder->setParseContent(false);
$mock = $this->getMock('JBBCode\DocumentElement', array('getChildren', 'getCodeDefinition'));
$mock->expects($this->never())
->method('getChildren');
$mock->expects($this->once())
->method('getCodeDefinition')
->will($this->returnValue(
$builder->build()
));
$this->_smileyVisitor->visitElementNode($mock);
$childMock = $this->getMock('JBBCode\ElementNode', array('accept', 'parseContent'));
$childMock->expects($this->once())
->method('accept')
->with($this->equalTo($this->_smileyVisitor));
$mock = $this->getMock('JBBCode\DocumentElement', array('getChildren', 'getCodeDefinition'));
$mock->expects($this->once())
->method('getChildren')
->will($this->returnValue(array(
$childMock,
)));
$mock->expects($this->once())
->method('getCodeDefinition')
->will($this->returnValue($builder->setParseContent(true)->build()));
$this->_smileyVisitor->visitElementNode($mock);
}
public function testVisitTextNodeEmpty()
{
$textNode = new JBBCode\TextNode('');
$textNode->accept($this->_smileyVisitor);
$this->assertEmpty($textNode->getValue());
}
/**
* @param $string
* @dataProvider smileyProvider()
*/
public function testVisitTextNode($string)
{
$textNode = new JBBCode\TextNode($string);
$textNode->accept($this->_smileyVisitor);
$this->assertNotFalse(strpos($textNode->getValue(), '<img src="/smiley.png" alt=":)" />'));
}
public function smileyProvider()
{
return array(
array( ':)'),
array( ':) foo'),
array( 'foo :)'),
array( 'foo :) bar'),
);
}
}

View file

@ -0,0 +1,77 @@
<?php
require_once dirname(dirname(__DIR__)) . '/visitors/TagCountingVisitor.php';
class TagCountingVisitorTest extends \PHPUnit_Framework_TestCase
{
/** @var JBBCode\visitors\TagCountingVisitor */
protected $_tagCountingVisitor;
protected function setUp()
{
$this->_tagCountingVisitor = new JBBCode\visitors\TagCountingVisitor();
}
public function testVisitTextNode()
{
$mock = $this->getMock('JBBCode\TextNode', array('accept'), array(''));
$mock->expects($this->never())
->method('accept');
$this->_tagCountingVisitor->visitTextNode($mock);
}
/**
* @covers JBBCode\visitors\TagCountingVisitor::getFrequency()
* @covers JBBCode\visitors\TagCountingVisitor::visitElementNode()
*/
public function testVisitElementNode()
{
$childMock = $this->getMock('JBBCode\ElementNode', array('accept'));
$childMock->expects($this->once())
->method('accept')
->with($this->equalTo($this->_tagCountingVisitor));
$mock = $this->getMock('JBBCode\ElementNode', array('getChildren', 'getTagName'));
$mock->expects($this->once())
->method('getChildren')
->will($this->returnValue(array(
$childMock,
)));
$mock->expects($this->once())
->method('getTagName')
->will($this->returnValue('foo'));
$this->assertEquals(0, $this->_tagCountingVisitor->getFrequency('foo'));
$this->_tagCountingVisitor->visitElementNode($mock);
$this->assertEquals(1, $this->_tagCountingVisitor->getFrequency('foo'));
$mock = $this->getMock('JBBCode\ElementNode', array('getChildren', 'getTagName'));
$mock->expects($this->once())
->method('getChildren')
->will($this->returnValue(array()));
$mock->expects($this->once())
->method('getTagName')
->will($this->returnValue('foo'));
$this->_tagCountingVisitor->visitElementNode($mock);
$this->assertEquals(2, $this->_tagCountingVisitor->getFrequency('foo'));
}
public function testVisitDocumentElement()
{
$childMock = $this->getMock('JBBCode\ElementNode', array('accept'));
$childMock->expects($this->once())
->method('accept')
->with($this->equalTo($this->_tagCountingVisitor));
$mock = $this->getMock('JBBCode\DocumentElement', array('getChildren'));
$mock->expects($this->once())
->method('getChildren')
->will($this->returnValue(array(
$childMock,
)));
$this->_tagCountingVisitor->visitDocumentElement($mock);
}
}