2009-02-12 10:38:43 +01:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
|
|
|
use SilverStripe\View\Parsers\ShortcodeHandler;
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2009-02-12 10:38:43 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-02-12 10:38:43 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBHTMLTextTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Set test handler
|
|
|
|
ShortcodeParser::get('htmltest')
|
|
|
|
->register('test_shortcode', array('DBHTMLTextTest_Shortcode', 'handle_shortcode'));
|
|
|
|
ShortcodeParser::set_active('htmltest');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
ShortcodeParser::set_active('default');
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2009-02-12 10:38:43 +01:00
|
|
|
/**
|
2016-06-17 08:49:23 +02:00
|
|
|
* Test {@link Text->LimitCharacters()}
|
2009-02-12 10:38:43 +01:00
|
|
|
*/
|
2016-06-17 08:49:23 +02:00
|
|
|
public function providerLimitCharacters()
|
|
|
|
{
|
|
|
|
// HTML characters are stripped safely
|
|
|
|
return [
|
|
|
|
['The little brown fox jumped over the lazy cow.', 'The little brown fox...'],
|
|
|
|
['<p>Short & Sweet</p>', 'Short & Sweet'],
|
|
|
|
['This text contains & in it', 'This text contains &...'],
|
|
|
|
];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
/**
|
|
|
|
* Test {@link DBHTMLText->LimitCharacters()}
|
|
|
|
* @dataProvider providerLimitCharacters
|
|
|
|
* @param string $originalValue
|
|
|
|
* @param string $expectedValue
|
|
|
|
*/
|
|
|
|
public function testLimitCharacters($originalValue, $expectedValue) {
|
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue);
|
|
|
|
$result = $textObj->obj('LimitCharacters')->forTemplate();
|
|
|
|
$this->assertEquals($expectedValue, $result);
|
2009-02-12 10:38:43 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerLimitCharactersToClosestWord()
|
|
|
|
{
|
|
|
|
// HTML is converted safely to plain text
|
|
|
|
return [
|
|
|
|
// Standard words limited, ellipsis added if truncated
|
|
|
|
['<p>Lorem ipsum dolor sit amet</p>', 24, 'Lorem ipsum dolor sit...'],
|
|
|
|
|
|
|
|
// Complete words less than the character limit don't get truncated, ellipsis not added
|
|
|
|
['<p>Lorem ipsum</p>', 24, 'Lorem ipsum'],
|
|
|
|
['<p>Lorem</p>', 24, 'Lorem'],
|
|
|
|
['', 24, ''], // No words produces nothing!
|
|
|
|
|
|
|
|
// Special characters are encoded safely
|
|
|
|
['Nice & Easy', 24, 'Nice & Easy'],
|
|
|
|
|
|
|
|
// HTML is safely converted to plain text
|
|
|
|
['<p>Lorem ipsum dolor sit amet</p>', 24, 'Lorem ipsum dolor sit...'],
|
|
|
|
['<p><span>Lorem ipsum dolor sit amet</span></p>', 24, 'Lorem ipsum dolor sit...'],
|
|
|
|
['<p>Lorem ipsum</p>', 24, 'Lorem ipsum'],
|
|
|
|
['Lorem & ipsum dolor sit amet', 24, 'Lorem & ipsum dolor sit...']
|
|
|
|
];
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
/**
|
|
|
|
* Test {@link DBHTMLText->LimitCharactersToClosestWord()}
|
|
|
|
* @dataProvider providerLimitCharactersToClosestWord
|
|
|
|
*
|
|
|
|
* @param string $originalValue Raw string input
|
|
|
|
* @param int $limit
|
|
|
|
* @param string $expectedValue Expected template value
|
|
|
|
*/
|
|
|
|
public function testLimitCharactersToClosestWord($originalValue, $limit, $expectedValue) {
|
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue);
|
|
|
|
$result = $textObj->obj('LimitCharactersToClosestWord', [$limit])->forTemplate();
|
|
|
|
$this->assertEquals($expectedValue, $result);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
public function providerSummary()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'<p>Should strip <b>tags, but leave</b> text</p>',
|
|
|
|
50,
|
|
|
|
'Should strip tags, but leave text',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Line breaks are preserved
|
|
|
|
'<p>Unclosed tags <br>should not phase it</p>',
|
|
|
|
50,
|
|
|
|
"Unclosed tags <br />\nshould not phase it",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Paragraphs converted to linebreak
|
|
|
|
'<p>Second paragraph</p><p>should not cause errors or appear in output</p>',
|
|
|
|
50,
|
|
|
|
"Second paragraph<br />\n<br />\nshould not cause errors or appear in output",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>',
|
|
|
|
50,
|
|
|
|
"Second paragraph<br />\n<br />\nshould not cause errors or appear in output",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
' <img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>',
|
|
|
|
50,
|
|
|
|
"Second paragraph<br />\n<br />\nshould not cause errors or appear in output",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<p><img src="remove me">example <img src="include me">text words hello<img src="hello"></p>',
|
|
|
|
50,
|
|
|
|
'example text words hello',
|
|
|
|
],
|
|
|
|
|
|
|
|
// Shorter limits
|
|
|
|
[
|
|
|
|
'<p>A long paragraph should be cut off if limit is set</p>',
|
|
|
|
5,
|
|
|
|
'A long paragraph should be...',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<p>No matter <i>how many <b>tags</b></i> are in it</p>',
|
|
|
|
5,
|
|
|
|
'No matter how many tags...',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<p>A sentence is. nicer than hard limits</p>',
|
|
|
|
5,
|
|
|
|
'A sentence is.',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerSummary
|
|
|
|
* @param string $originalValue
|
|
|
|
* @param int $limit
|
|
|
|
* @param string $expectedValue
|
|
|
|
*/
|
|
|
|
public function testSummary($originalValue, $limit, $expectedValue) {
|
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue);
|
|
|
|
$result = $textObj->obj('Summary', [$limit])->forTemplate();
|
|
|
|
$this->assertEquals($expectedValue, $result);
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryEndings() {
|
2009-06-17 13:36:49 +02:00
|
|
|
$cases = array(
|
2016-06-17 08:49:23 +02:00
|
|
|
'...',
|
|
|
|
' -> more',
|
|
|
|
''
|
2009-06-17 13:36:49 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
$orig = '<p>Cut it off, cut it off</p>';
|
|
|
|
$match = 'Cut it off, cut';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
foreach($cases as $add) {
|
2016-06-17 08:49:23 +02:00
|
|
|
$textObj = DBField::create_field('HTMLFragment', $orig);
|
|
|
|
$result = $textObj->obj('Summary', [4, $add])->forTemplate();
|
|
|
|
$this->assertEquals($match.Convert::raw2xml($add), $result);
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
|
|
|
|
public function providerFirstSentence()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// Same behaviour as DBTextTest
|
|
|
|
['', ''],
|
|
|
|
['First sentence.', 'First sentence.'],
|
|
|
|
['First sentence. Second sentence', 'First sentence.'],
|
|
|
|
['First sentence? Second sentence', 'First sentence?'],
|
|
|
|
['First sentence! Second sentence', 'First sentence!'],
|
|
|
|
|
|
|
|
// DBHTHLText strips HTML first
|
|
|
|
['<br />First sentence.', 'First sentence.'],
|
|
|
|
['<p>First sentence. Second sentence. Third sentence</p>', 'First sentence.'],
|
|
|
|
];
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerFirstSentence
|
|
|
|
* @param string $originalValue
|
|
|
|
* @param string $expectedValue
|
|
|
|
*/
|
|
|
|
public function testFirstSentence($originalValue, $expectedValue) {
|
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue);
|
|
|
|
$result = $textObj->obj('FirstSentence')->forTemplate();
|
|
|
|
$this->assertEquals($expectedValue, $result);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-12 08:51:08 +02:00
|
|
|
public function testCreate() {
|
|
|
|
/** @var DBHTMLText $field */
|
|
|
|
$field = Object::create_from_string("HTMLFragment(['whitelist' => 'link'])", 'MyField');
|
|
|
|
$this->assertEquals(['link'], $field->getWhitelist());
|
|
|
|
$field = Object::create_from_string("HTMLFragment(['whitelist' => 'link,a'])", 'MyField');
|
|
|
|
$this->assertEquals(['link', 'a'], $field->getWhitelist());
|
|
|
|
$field = Object::create_from_string("HTMLFragment(['whitelist' => ['link', 'a']])", 'MyField');
|
|
|
|
$this->assertEquals(['link', 'a'], $field->getWhitelist());
|
|
|
|
$field = Object::create_from_string("HTMLFragment", 'MyField');
|
|
|
|
$this->assertEmpty($field->getWhitelist());
|
|
|
|
|
|
|
|
// Test shortcodes
|
|
|
|
$field = Object::create_from_string("HTMLFragment(['shortcodes' => true])", 'MyField');
|
|
|
|
$this->assertEquals(true, $field->getProcessShortcodes());
|
|
|
|
$field = Object::create_from_string("HTMLFragment(['shortcodes' => false])", 'MyField');
|
|
|
|
$this->assertEquals(false, $field->getProcessShortcodes());
|
|
|
|
|
|
|
|
// Mix options
|
|
|
|
$field = Object::create_from_string("HTMLFragment(['shortcodes' => true, 'whitelist' => ['a'])", 'MyField');
|
|
|
|
$this->assertEquals(true, $field->getProcessShortcodes());
|
|
|
|
$this->assertEquals(['a'], $field->getWhitelist());
|
|
|
|
}
|
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
public function providerToPlain()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'<p><img />Lots of <strong>HTML <i>nested</i></strong> tags',
|
|
|
|
'Lots of HTML nested tags',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<p>Multi</p><p>Paragraph<br>Also has multilines.</p>',
|
|
|
|
"Multi\n\nParagraph\nAlso has multilines.",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'<p>Collapses</p><p></p><p>Excessive<br/><br /><br>Newlines</p>',
|
|
|
|
"Collapses\n\nExcessive\n\nNewlines",
|
|
|
|
]
|
|
|
|
];
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerToPlain
|
|
|
|
* @param string $html
|
|
|
|
* @param string $plain
|
|
|
|
*/
|
|
|
|
public function testToPlain($html, $plain) {
|
|
|
|
/** @var DBHTMLText $textObj */
|
|
|
|
$textObj = DBField::create_field('HTMLFragment', $html);
|
|
|
|
$this->assertEquals($plain, $textObj->Plain());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* each test is in the format input, charactere limit, highlight, expected output
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerContextSummary()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'This is some text. It is a test',
|
|
|
|
20,
|
|
|
|
'test',
|
|
|
|
'... text. It is a <span class="highlight">test</span>'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Retains case of original string
|
|
|
|
'This is some test text. Test test what if you have multiple keywords.',
|
|
|
|
50,
|
|
|
|
'some test',
|
|
|
|
'This is <span class="highlight">some</span> <span class="highlight">test</span> text.'
|
|
|
|
. ' <span class="highlight">Test</span> <span class="highlight">test</span> what if you have...'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'Here is some text & HTML included',
|
|
|
|
20,
|
|
|
|
'html',
|
|
|
|
'... text & <span class="highlight">HTML</span> inc...'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'A dog ate a cat while looking at a Foobar',
|
|
|
|
100,
|
|
|
|
'a',
|
|
|
|
// test that it does not highlight too much (eg every a)
|
|
|
|
'A dog ate a cat while looking at a Foobar',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'A dog ate a cat while looking at a Foobar',
|
|
|
|
100,
|
|
|
|
'ate',
|
|
|
|
// it should highlight 3 letters or more.
|
|
|
|
'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
|
|
|
|
],
|
|
|
|
|
|
|
|
// HTML Content is plain-textified, and incorrect tags removed
|
|
|
|
[
|
|
|
|
'<p>A dog ate a cat while <span class="highlight">looking</span> at a Foobar</p>',
|
|
|
|
100,
|
|
|
|
'ate',
|
|
|
|
// it should highlight 3 letters or more.
|
|
|
|
'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerContextSummary
|
|
|
|
* @param string $originalValue Input
|
|
|
|
* @param int $limit Numer of characters
|
|
|
|
* @param string $keywords Keywords to highlight
|
|
|
|
* @param string $expectedValue Expected output (XML encoded safely)
|
|
|
|
*/
|
|
|
|
public function testContextSummary($originalValue, $limit, $keywords, $expectedValue)
|
|
|
|
{
|
|
|
|
$text = DBField::create_field('HTMLFragment', $originalValue);
|
|
|
|
$result = $text->obj('ContextSummary', [$limit, $keywords])->forTemplate();
|
|
|
|
// it should highlight 3 letters or more.
|
|
|
|
$this->assertEquals($expectedValue, $result);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-01-31 01:07:24 +01:00
|
|
|
|
|
|
|
public function testRAW() {
|
2016-06-17 08:49:23 +02:00
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This');
|
|
|
|
$this->assertEquals('This & This', $data->RAW());
|
2012-01-31 01:07:24 +01:00
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This');
|
|
|
|
$this->assertEquals('This & This', $data->RAW());
|
2012-01-31 01:07:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-31 01:07:24 +01:00
|
|
|
public function testXML() {
|
2016-06-17 08:49:23 +02:00
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This');
|
|
|
|
$this->assertEquals('This & This', $data->XML());
|
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This');
|
|
|
|
$this->assertEquals('This &amp; This', $data->XML());
|
2012-01-31 01:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHTML() {
|
2016-06-17 08:49:23 +02:00
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This');
|
|
|
|
$this->assertEquals('This & This', $data->HTML());
|
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This');
|
|
|
|
$this->assertEquals('This &amp; This', $data->HTML());
|
2012-01-31 01:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testJS() {
|
2016-06-17 08:49:23 +02:00
|
|
|
$data = DBField::create_field('HTMLText', '"this is & test"');
|
|
|
|
$this->assertEquals('\"this is \x26amp; test\"', $data->JS());
|
2012-01-31 01:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testATT() {
|
2016-06-17 08:49:23 +02:00
|
|
|
// HTML Fragment
|
|
|
|
$data = DBField::create_field('HTMLFragment', '"this is a test"');
|
|
|
|
$this->assertEquals('"this is a test"', $data->ATT());
|
|
|
|
|
|
|
|
// HTML Text (passes shortcodes + tidy)
|
2016-07-12 08:51:08 +02:00
|
|
|
$data = DBField::create_field('HTMLText', '"');
|
2016-06-17 08:49:23 +02:00
|
|
|
$this->assertEquals('"', $data->ATT());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShortcodesProcessed()
|
|
|
|
{
|
|
|
|
/** @var DBHTMLText $obj */
|
|
|
|
$obj = DBField::create_field(
|
|
|
|
'HTMLText',
|
|
|
|
'<p>Some content <strong>[test_shortcode]</strong> with shortcode</p>'
|
|
|
|
);
|
|
|
|
// Basic DBField methods process shortcodes
|
|
|
|
$this->assertEquals(
|
|
|
|
'Some content shortcode content with shortcode',
|
|
|
|
$obj->Plain()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Some content <strong>shortcode content</strong> with shortcode</p>',
|
|
|
|
$obj->RAW()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Some content <strong>shortcode content</strong> with shortcode</p>',
|
|
|
|
$obj->XML()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Some content <strong>shortcode content</strong> with shortcode</p>',
|
|
|
|
$obj->HTML()
|
|
|
|
);
|
|
|
|
// Test summary methods
|
|
|
|
$this->assertEquals(
|
|
|
|
'Some content shortcode...',
|
|
|
|
$obj->Summary(3)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Some content shortcode content with shortcode',
|
|
|
|
$obj->LimitSentences(1)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Some content shortco...',
|
|
|
|
$obj->LimitCharacters(20)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-12 06:44:47 +01:00
|
|
|
function testExists() {
|
2015-08-30 07:02:55 +02:00
|
|
|
$h = new DBHTMLText();
|
2011-12-12 06:44:47 +01:00
|
|
|
$h->setValue("");
|
|
|
|
$this->assertFalse($h->exists());
|
2016-07-12 08:51:08 +02:00
|
|
|
$h->setValue("<p>content</p>");
|
2011-12-12 06:44:47 +01:00
|
|
|
$this->assertTrue($h->exists());
|
|
|
|
}
|
2014-02-16 23:22:26 +01:00
|
|
|
|
|
|
|
function testWhitelist() {
|
2016-07-12 08:51:08 +02:00
|
|
|
$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link']);
|
2014-02-16 23:22:26 +01:00
|
|
|
$this->assertEquals(
|
2014-04-09 04:48:02 +02:00
|
|
|
'<meta content="Keep"><link href="Also Keep">',
|
|
|
|
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Remove Text'),
|
|
|
|
'Removes any elements not in whitelist excluding text elements'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-12 08:51:08 +02:00
|
|
|
$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link,text()']);
|
2014-04-09 04:48:02 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'<meta content="Keep"><link href="Also Keep">Keep Text',
|
|
|
|
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Keep Text'),
|
|
|
|
'Removes any elements not in whitelist including text elements'
|
2014-02-16 23:22:26 +01:00
|
|
|
);
|
|
|
|
}
|
2016-06-30 16:46:23 +02:00
|
|
|
|
|
|
|
public function testShortCodeParsedInRAW() {
|
|
|
|
$parser = ShortcodeParser::get('HTMLTextTest');
|
|
|
|
$parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) {
|
|
|
|
return 'replaced';
|
|
|
|
});
|
|
|
|
ShortcodeParser::set_active('HTMLTextTest');
|
2016-07-11 22:30:15 +02:00
|
|
|
/** @var DBHTMLText $field */
|
2016-06-30 16:46:23 +02:00
|
|
|
$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
|
|
|
|
$this->assertEquals('<p>replaced</p>', $field->RAW());
|
|
|
|
$this->assertEquals('<p>replaced</p>', (string)$field);
|
|
|
|
|
|
|
|
$field->setOptions(array(
|
|
|
|
'shortcodes' => false,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals('<p>[shortcode]</p>', $field->RAW());
|
|
|
|
$this->assertEquals('<p>[shortcode]</p>', (string)$field);
|
|
|
|
|
|
|
|
|
2016-06-30 16:54:44 +02:00
|
|
|
ShortcodeParser::set_active('default');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShortCodeParsedInTemplateHelpers() {
|
|
|
|
$parser = ShortcodeParser::get('HTMLTextTest');
|
|
|
|
$parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) {
|
|
|
|
return 'Replaced short code with this. <a href="home">home</a>';
|
|
|
|
});
|
|
|
|
ShortcodeParser::set_active('HTMLTextTest');
|
2016-07-11 22:30:15 +02:00
|
|
|
/** @var DBHTMLText $field */
|
2016-06-30 16:54:44 +02:00
|
|
|
$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->HTMLATT()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'%3Cp%3EReplaced+short+code+with+this.+%3Ca+href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E',
|
|
|
|
$field->URLATT()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'%3Cp%3EReplaced%20short%20code%20with%20this.%20%3Ca%20href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E',
|
|
|
|
$field->RAWURLATT()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->ATT()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->RAW()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'\x3cp\x3eReplaced short code with this. \x3ca href=\"home\"\x3ehome\x3c/a\x3e\x3c/p\x3e',
|
|
|
|
$field->JS()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->HTML()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->XML()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Repl...',
|
|
|
|
$field->LimitCharacters(4, '...')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced...',
|
|
|
|
$field->LimitCharactersToClosestWord(10, '...')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced...',
|
|
|
|
$field->LimitWordCount(1, '...')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>replaced short code with this. <a href="home">home</a></p>',
|
|
|
|
$field->LowerCase()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<P>REPLACED SHORT CODE WITH THIS. <A HREF="HOME">HOME</A></P>',
|
|
|
|
$field->UpperCase()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced short code with this. home',
|
2016-07-12 08:51:08 +02:00
|
|
|
$field->Plain()
|
2016-06-30 16:54:44 +02:00
|
|
|
);
|
|
|
|
Config::nest();
|
2016-08-19 00:51:35 +02:00
|
|
|
Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://example.com/');
|
2016-06-30 16:54:44 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Replaced short code with this. <a href="http://example.com/home">home</a></p>',
|
|
|
|
$field->AbsoluteLinks()
|
|
|
|
);
|
|
|
|
Config::unnest();
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced short code with this.',
|
|
|
|
$field->LimitSentences(1)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced short code with this.',
|
|
|
|
$field->FirstSentence()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced short...',
|
|
|
|
$field->Summary(2)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-07-12 08:51:08 +02:00
|
|
|
'Replaced short code with this. home',
|
2016-06-30 16:54:44 +02:00
|
|
|
$field->FirstParagraph()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Replaced <span class="highlight">short</span> <span class="highlight">code</span> with this. home',
|
|
|
|
$field->ContextSummary(500, 'short code')
|
|
|
|
);
|
|
|
|
|
2016-06-30 16:46:23 +02:00
|
|
|
ShortcodeParser::set_active('default');
|
|
|
|
}
|
2009-02-12 10:38:43 +01:00
|
|
|
}
|
2016-06-17 08:49:23 +02:00
|
|
|
|
|
|
|
class DBHTMLTextTest_Shortcode implements ShortcodeHandler, TestOnly {
|
|
|
|
public static function get_shortcodes()
|
|
|
|
{
|
|
|
|
return 'test';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
|
|
|
|
{
|
|
|
|
return 'shortcode content';
|
|
|
|
}
|
|
|
|
}
|