summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/helpers
diff options
context:
space:
mode:
authorGreg Aker <greg.aker@ellislab.com>2011-04-21 22:19:37 +0200
committerGreg Aker <greg.aker@ellislab.com>2011-04-21 22:19:37 +0200
commitb58aeaca9be4b342bb6d88138005fdc7138828b4 (patch)
treef986aefeec38cfb6938c93f90596c41631edd7ce /tests/codeigniter/helpers
parent1d3021a26e3d542137ceddc6c0f4a08a4f80a096 (diff)
parent9512ab8a22ff14a3789cba6e5ace03aed4196e23 (diff)
Branch merge
Diffstat (limited to 'tests/codeigniter/helpers')
-rw-r--r--tests/codeigniter/helpers/array_helper_test.php49
-rw-r--r--tests/codeigniter/helpers/html_helper_test.php67
-rw-r--r--tests/codeigniter/helpers/inflector_helper_test.php91
-rw-r--r--tests/codeigniter/helpers/string_helper_test.php117
-rw-r--r--tests/codeigniter/helpers/text_helper_test.php151
5 files changed, 475 insertions, 0 deletions
diff --git a/tests/codeigniter/helpers/array_helper_test.php b/tests/codeigniter/helpers/array_helper_test.php
new file mode 100644
index 000000000..fd306cee8
--- /dev/null
+++ b/tests/codeigniter/helpers/array_helper_test.php
@@ -0,0 +1,49 @@
+<?php
+
+// OLD TEST FORMAT: DO NOT COPY
+
+require_once(BASEPATH.'helpers/array_helper.php');
+
+class Array_helper_test extends CI_TestCase
+{
+ public function setUp()
+ {
+ $this->my_array = array(
+ 'foo' => 'bar',
+ 'sally' => 'jim',
+ 'maggie' => 'bessie',
+ 'herb' => 'cook'
+ );
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testElementWithExistingItem()
+ {
+ $this->assertEquals(FALSE, element('testing', $this->my_array));
+
+ $this->assertEquals('not set', element('testing', $this->my_array, 'not set'));
+
+ $this->assertEquals('bar', element('foo', $this->my_array));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testRandomElement()
+ {
+ // Send a string, not an array to random_element
+ $this->assertEquals('my string', random_element('my string'));
+
+ // Test sending an array
+ $this->assertEquals(TRUE, in_array(random_element($this->my_array), $this->my_array));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testElements()
+ {
+ $this->assertEquals(TRUE, is_array(elements('test', $this->my_array)));
+ $this->assertEquals(TRUE, is_array(elements('foo', $this->my_array)));
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/html_helper_test.php b/tests/codeigniter/helpers/html_helper_test.php
new file mode 100644
index 000000000..8c0e53301
--- /dev/null
+++ b/tests/codeigniter/helpers/html_helper_test.php
@@ -0,0 +1,67 @@
+<?php
+
+require_once(BASEPATH.'helpers/html_helper.php');
+
+class Html_helper_test extends CI_TestCase
+{
+ public function testHeading()
+ {
+ $this->assertEquals('<h1>foobar</h1>', heading('foobar'));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testUl()
+ {
+ $expect = <<<EOH
+<ul>
+ <li>foo</li>
+ <li>bar</li>
+</ul>
+
+EOH;
+
+ $expect = ltrim($expect);
+
+ $list = array('foo', 'bar');
+
+ $this->assertEquals($expect, ul($list));
+
+
+ $expect = <<<EOH
+<ul class="test">
+ <li>foo</li>
+ <li>bar</li>
+</ul>
+
+EOH;
+
+ $expect = ltrim($expect);
+
+ $list = array('foo', 'bar');
+
+ $this->assertEquals($expect, ul($list, ' class="test"'));
+
+ $this->assertEquals($expect, ul($list, array('class' => 'test')));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testNBS()
+ {
+ $this->assertEquals('&nbsp;&nbsp;&nbsp;', nbs(3));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testMeta()
+ {
+ $this->assertEquals("<meta name=\"test\" content=\"foo\" />\n", meta('test', 'foo'));
+
+ $expect = "<meta name=\"foo\" content=\"\" />\n";
+
+ $this->assertEquals($expect, meta(array('name' => 'foo')));
+
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/inflector_helper_test.php b/tests/codeigniter/helpers/inflector_helper_test.php
new file mode 100644
index 000000000..e59875e4a
--- /dev/null
+++ b/tests/codeigniter/helpers/inflector_helper_test.php
@@ -0,0 +1,91 @@
+<?php
+
+require_once(BASEPATH.'helpers/inflector_helper.php');
+
+class Inflector_helper_test extends CI_TestCase {
+
+
+ public function testSingular()
+ {
+ $strs = array(
+ 'tellies' => 'telly',
+ 'smellies' => 'smelly',
+ 'abjectnesses' => 'abjectness',
+ 'smells' => 'smell'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, singular($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testPlural()
+ {
+ $strs = array(
+ 'telly' => 'tellies',
+ 'smelly' => 'smellies',
+ 'abjectness' => 'abjectness',
+ 'smell' => 'smells',
+ 'witch' => 'witches'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, plural($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testCamelize()
+ {
+ $strs = array(
+ 'this is the string' => 'thisIsTheString',
+ 'this is another one' => 'thisIsAnotherOne',
+ 'i-am-playing-a-trick' => 'i-am-playing-a-trick',
+ 'what_do_you_think-yo?' => 'whatDoYouThink-yo?',
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, camelize($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testUnderscore()
+ {
+ $strs = array(
+ 'this is the string' => 'this_is_the_string',
+ 'this is another one' => 'this_is_another_one',
+ 'i-am-playing-a-trick' => 'i-am-playing-a-trick',
+ 'what_do_you_think-yo?' => 'what_do_you_think-yo?',
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, underscore($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testHumanize()
+ {
+ $strs = array(
+ 'this_is_the_string' => 'This Is The String',
+ 'this_is_another_one' => 'This Is Another One',
+ 'i-am-playing-a-trick' => 'I-am-playing-a-trick',
+ 'what_do_you_think-yo?' => 'What Do You Think-yo?',
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, humanize($str));
+ }
+ }
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/string_helper_test.php b/tests/codeigniter/helpers/string_helper_test.php
new file mode 100644
index 000000000..00ba5dec7
--- /dev/null
+++ b/tests/codeigniter/helpers/string_helper_test.php
@@ -0,0 +1,117 @@
+<?php
+
+require_once(BASEPATH.'helpers/string_helper.php');
+
+class String_helper_test extends CI_TestCase
+{
+ public function testTrimSlashes()
+ {
+ $strs = array(
+ '//Slashes//\/' => 'Slashes//\\',
+ '/var/www/html/' => 'var/www/html'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, trim_slashes($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testStripSlashes()
+ {
+ $this->assertEquals("This is totally foo bar'd", trim_slashes("This is totally foo bar'd"));
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testStripQuotes()
+ {
+ $strs = array(
+ '"me oh my!"' => 'me oh my!',
+ "it's a winner!" => 'its a winner!',
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, strip_quotes($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testQuotesToEntities()
+ {
+ $strs = array(
+ '"me oh my!"' => '&quot;me oh my!&quot;',
+ "it's a winner!" => 'it&#39;s a winner!',
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, quotes_to_entities($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testReduceDoubleSlashes()
+ {
+ $strs = array(
+ 'http://codeigniter.com' => 'http://codeigniter.com',
+ '//var/www/html/example.com/' => '/var/www/html/example.com/',
+ '/var/www/html//index.php' => '/var/www/html/index.php'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, reduce_double_slashes($str));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testReduceMultiples()
+ {
+ $strs = array(
+ 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
+ 'Ringo, John, Paul,,' => 'Ringo, John, Paul,'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, reduce_multiples($str));
+ }
+
+ $strs = array(
+ 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
+ 'Ringo, John, Paul,,' => 'Ringo, John, Paul'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, reduce_multiples($str, ',', TRUE));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ public function testRepeater()
+ {
+ $strs = array(
+ 'a' => 'aaaaaaaaaa',
+ '&nbsp;' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
+ '<br>' => '<br><br><br><br><br><br><br><br><br><br>'
+
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, repeater($str, 10));
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php
new file mode 100644
index 000000000..22c834bcf
--- /dev/null
+++ b/tests/codeigniter/helpers/text_helper_test.php
@@ -0,0 +1,151 @@
+<?php
+
+require_once(BASEPATH.'helpers/text_helper.php');
+
+class Text_helper_test extends CI_TestCase
+{
+ private $_long_string;
+
+ public function setUp()
+ {
+ $this->_long_string = 'Once upon a time, a framework had no tests. It sad. So some nice people began to write tests. The more time that went on, the happier it became. Everyone was happy.';
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testWordLimiter()
+ {
+ $this->assertEquals('Once upon a time,&#8230;', word_limiter($this->_long_string, 4));
+ $this->assertEquals('Once upon a time,&hellip;', word_limiter($this->_long_string, 4, '&hellip;'));
+ $this->assertEquals('', word_limiter('', 4));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testCharacterLimiter()
+ {
+ $this->assertEquals('Once upon a time, a&#8230;', character_limiter($this->_long_string, 20));
+ $this->assertEquals('Once upon a time, a&hellip;', character_limiter($this->_long_string, 20, '&hellip;'));
+ $this->assertEquals('Short', character_limiter('Short', 20));
+ $this->assertEquals('Short', character_limiter('Short', 5));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testAsciiToEntities()
+ {
+ $strs = array(
+ '“‘ “test”' => '&#8220;&#8216; &#8220;test&#8221;',
+ '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, ascii_to_entities($str));
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testEntitiesToAscii()
+ {
+ $strs = array(
+ '&#8220;&#8216; &#8220;test&#8221;' => '“‘ “test”',
+ '&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;' => '†¥¨ˆøåß∂ƒ©˙∆˚¬'
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, entities_to_ascii($str));
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testCensoredWords()
+ {
+ $censored = array('boob', 'nerd', 'ass', 'fart');
+
+ $strs = array(
+ 'Ted bobbled the ball' => 'Ted bobbled the ball',
+ 'Jake is a nerdo' => 'Jake is a nerdo',
+ 'The borg will assimilate you' => 'The borg will assimilate you',
+ 'Did Mary Fart?' => 'Did Mary $*#?',
+ 'Jake is really a boob' => 'Jake is really a $*#'
+ );
+
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, word_censor($str, $censored, '$*#'));
+ }
+
+ // test censored words being sent as a string
+ $this->assertEquals('test', word_censor('test', 'test'));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testHighlightCode()
+ {
+ $code = '<?php var_dump($this); ?>';
+ $expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\">&lt;?php&nbsp;var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">);&nbsp;</span><span style=\"color: #0000BB\">?&gt;&nbsp;</span>\n</span>\n</code>";
+
+ $this->assertEquals($expect, highlight_code($code));
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testHighlightPhrase()
+ {
+ $strs = array(
+ 'this is a phrase' => '<strong>this is</strong> a phrase',
+ 'this is another' => '<strong>this is</strong> another',
+ 'Gimme a test, Sally' => 'Gimme a test, Sally',
+ 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>',
+ '' => ''
+ );
+
+ foreach ($strs as $str => $expect)
+ {
+ $this->assertEquals($expect, highlight_phrase($str, 'this is'));
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ public function testEllipsizing()
+ {
+ $strs = array(
+ '0' => array(
+ 'this is my string' => '&hellip; my string',
+ "here's another one" => '&hellip;nother one',
+ 'this one is just a bit longer' => '&hellip;bit longer',
+ 'short' => 'short'
+ ),
+ '.5' => array(
+ 'this is my string' => 'this &hellip;tring',
+ "here's another one" => "here'&hellip;r one",
+ 'this one is just a bit longer' => 'this &hellip;onger',
+ 'short' => 'short'
+ ),
+ '1' => array(
+ 'this is my string' => 'this is my&hellip;',
+ "here's another one" => "here's ano&hellip;",
+ 'this one is just a bit longer' => 'this one i&hellip;',
+ 'short' => 'short'
+ ),
+ );
+
+ foreach ($strs as $pos => $s)
+ {
+ foreach ($s as $str => $expect)
+ {
+ $this->assertEquals($expect, ellipsize($str, 10, $pos));
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+} \ No newline at end of file