public function transform($text, CM_Frontend_Render $render) { $text = (string) $text; $badwordList = new CM_Paging_ContentList_Badwords(); $text = $badwordList->replaceMatch($text, '…'); return $text; }
public function testValidateBadwords() { $badwordsList = new CM_Paging_ContentList_Badwords(); $field = new CM_FormField_Text(['name' => 'foo', 'forbidBadwords' => true]); $environment = new CM_Frontend_Environment(); $render = new CM_Frontend_Render(); try { $field->validate($environment, 'foo'); } catch (CM_Exception_FormFieldValidation $ex) { $this->fail('Expected value not to be a badword'); } $badwordsList->add('foo'); try { $field->validate($environment, 'foo'); $this->fail('Expected value to be a badword'); } catch (CM_Exception_FormFieldValidation $ex) { $this->assertContains('The word `foo` is not allowed', $ex->getMessagePublic($render)); } $field = new CM_FormField_Text(['name' => 'foo', 'forbidBadwords' => false]); try { $field->validate($environment, 'foo'); } catch (CM_Exception_FormFieldValidation $ex) { $this->fail('Badword-validation shouldn\'t be activated'); } }
public function testTransformCustomReplacement() { $filter = new CM_Usertext_Filter_Badwords('---'); $badwords = new CM_Paging_ContentList_Badwords(); $badwords->add('foo'); $this->assertSame('--- bar', $filter->transform('foo bar', new CM_Frontend_Render())); }
public function testReplaceMatch() { $this->assertSame('hallo … world.', $this->_paging->replaceMatch('hallo foo-bar world.', '…')); $this->assertSame('hallo $1 \\1 world.', $this->_paging->replaceMatch('hallo foo-bar world.', '$1 \\1')); $this->assertSame('Hello … world.', $this->_paging->replaceMatch('Hello zoo world.', '…')); $this->assertSame('Hello … world.', $this->_paging->replaceMatch('Hello sub.zoo.com world.', '…')); }
public function testAllowBadwords() { $usertext = new CM_Usertext_Usertext(); $usertext->setMode('escape', ['allowBadwords' => true]); $badwordList = new CM_Paging_ContentList_Badwords(); $badWord = 'testBad'; $badwordList->add($badWord); $sentString = 'Hello i am ' . $badWord . ' !'; $this->assertSame($sentString, $usertext->transform($sentString, new CM_Frontend_Render())); }
public function validate(CM_Frontend_Environment $environment, $userInput) { if (isset($this->_options['lengthMax']) && mb_strlen($userInput) > $this->_options['lengthMax']) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Too long')); } if (isset($this->_options['lengthMin']) && mb_strlen($userInput) < $this->_options['lengthMin']) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('Too short')); } if (!empty($this->_options['forbidBadwords'])) { $badwordList = new CM_Paging_ContentList_Badwords(); if ($badword = $badwordList->getMatch($userInput)) { throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('The word `{$badword}` is not allowed', ['badword' => $badword])); } } return $userInput; }
public function testProcess() { $replace = '…'; $badwords = new CM_Paging_ContentList_Badwords(); $filter = new CM_Usertext_Filter_Badwords(); $render = new CM_Frontend_Render(); $actual = $filter->transform("hello foo there", $render); $this->assertSame("hello foo there", $actual); $badwords->add('foo'); $badwords->add('x … x'); $badwords->add('f(o-].)o'); $badwords->add('bar'); $badwords->add('foobar'); $badwords->add('zoo*far'); CMTest_TH::clearCache(); $actual = $filter->transform("hello foo there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello x foo x there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello Foo there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello foot there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello f(o-].)o there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello bar there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello bart there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello bar3 there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello bartender there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello bar.de there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello bar. there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello foobar there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello XfoobarX there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello mayo.foobar.ran there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello zoofar there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello zoo!!far there", $render); $this->assertSame("hello {$replace} there", $actual); $actual = $filter->transform("hello zoo far there", $render); $this->assertSame("hello {$replace} there", $actual); }