Fixing String::insert() to allow '.' in parameters, added tests to show usage of String::cleanInsert(). Closes #5627

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7764 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-10-18 23:56:41 +00:00
parent f9f1c4df50
commit 5f0fd5241e
2 changed files with 38 additions and 2 deletions

View file

@ -210,6 +210,7 @@ class String extends Object {
* @param string $options An array of options, see description above
* @return string
* @access public
* @static
*/
function insert($str, $data, $options = array()) {
$options = array_merge(
@ -267,6 +268,7 @@ class String extends Object {
* @param string $options
* @return string
* @access public
* @static
*/
function cleanInsert($str, $options) {
$clean = $options['clean'];
@ -282,7 +284,7 @@ class String extends Object {
switch ($clean['method']) {
case 'html':
$clean = array_merge(array(
'word' => '[\w,]+',
'word' => '[\w,.]+',
'andText' => true,
'replacement' => '',
), $clean);
@ -300,7 +302,7 @@ class String extends Object {
break;
case 'text':
$clean = array_merge(array(
'word' => '[\w,]+',
'word' => '[\w,.]+',
'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
'replacement' => '',
), $clean);

View file

@ -177,6 +177,40 @@ class StringTest extends CakeTestCase {
$expected = "I :verb cake. cake is fantastic.";
$this->assertEqual($result, $expected);
$result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => array('replacement' => ' of course', 'method' => 'text')));
$expected = "We are of course passing.";
$this->assertEqual($result, $expected);
$result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => true));
$expected = "We are passing.";
$this->assertEqual($result, $expected);
}
/**
* test Clean Insert
*
* @return void
**/
function testCleanInsert() {
$result = String::cleanInsert(':incomplete', array('clean' => true, 'before' => ':', 'after' => ''));
$this->assertEqual($result, '');
$result = String::cleanInsert(':incomplete', array(
'clean' => array('method' => 'text', 'replacement' => 'complete'),
'before' => ':', 'after' => '')
);
$this->assertEqual($result, 'complete');
$result = String::cleanInsert(':in.complete', array('clean' => true, 'before' => ':', 'after' => ''));
$this->assertEqual($result, '');
$result = String::cleanInsert(':in.complete and', array('clean' => true, 'before' => ':', 'after' => ''));
$this->assertEqual($result, '');
$result = String::cleanInsert(':in.complete or stuff', array('clean' => true, 'before' => ':', 'after' => ''));
$this->assertEqual($result, 'stuff');
$result = String::cleanInsert('<p class=":missing" id=":missing">Text here</p>', array('clean' => 'html', 'before' => ':', 'after' => ''));
$this->assertEqual($result, '<p>Text here</p>');
}
/**
* testTokenize method