blob: 75701ec1392f75f099045d6005371b9a08c3ae4a [file] [log] [blame]
Greg Aker052b01d2011-04-21 14:38:03 -05001<?php
2
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07003class String_helper_test extends CI_TestCase {
Greg Aker052b01d2011-04-21 14:38:03 -05004
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07005 public function set_up()
6 {
7 $this->helper('string');
8 }
9
tiyowan3fb5aa52012-03-16 19:08:34 +040010 public function test_strip_slashes()
11 {
12 $expected = array(
Andrey Andreev99b782d2012-06-09 22:24:46 +030013 "Is your name O'reilly?",
tiyowan3fb5aa52012-03-16 19:08:34 +040014 "No, my name is O'connor."
15 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030016
tiyowan3fb5aa52012-03-16 19:08:34 +040017 $str = array(
18 "Is your name O\'reilly?",
19 "No, my name is O\'connor."
20 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030021
tiyowan3fb5aa52012-03-16 19:08:34 +040022 $this->assertEquals($expected, strip_slashes($str));
23 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030024
Eric Barnes68286a42011-04-21 22:00:33 -040025 public function test_trim_slashes()
Greg Aker052b01d2011-04-21 14:38:03 -050026 {
27 $strs = array(
28 '//Slashes//\/' => 'Slashes//\\',
29 '/var/www/html/' => 'var/www/html'
30 );
Eric Barnes78ec0602011-11-27 00:48:56 -050031
Greg Aker052b01d2011-04-21 14:38:03 -050032 foreach ($strs as $str => $expect)
33 {
34 $this->assertEquals($expect, trim_slashes($str));
35 }
36 }
Eric Barnes78ec0602011-11-27 00:48:56 -050037
38 // --------------------------------------------------------------------
Greg Aker052b01d2011-04-21 14:38:03 -050039
Eric Barnes68286a42011-04-21 22:00:33 -040040 public function test_strip_quotes()
Greg Aker052b01d2011-04-21 14:38:03 -050041 {
42 $strs = array(
43 '"me oh my!"' => 'me oh my!',
44 "it's a winner!" => 'its a winner!',
45 );
Eric Barnes78ec0602011-11-27 00:48:56 -050046
Greg Aker052b01d2011-04-21 14:38:03 -050047 foreach ($strs as $str => $expect)
48 {
49 $this->assertEquals($expect, strip_quotes($str));
50 }
51 }
52
Eric Barnes78ec0602011-11-27 00:48:56 -050053 // --------------------------------------------------------------------
54
Eric Barnes68286a42011-04-21 22:00:33 -040055 public function test_quotes_to_entities()
Greg Aker052b01d2011-04-21 14:38:03 -050056 {
57 $strs = array(
58 '"me oh my!"' => '&quot;me oh my!&quot;',
59 "it's a winner!" => 'it&#39;s a winner!',
60 );
Eric Barnes78ec0602011-11-27 00:48:56 -050061
Greg Aker052b01d2011-04-21 14:38:03 -050062 foreach ($strs as $str => $expect)
63 {
64 $this->assertEquals($expect, quotes_to_entities($str));
Eric Barnes78ec0602011-11-27 00:48:56 -050065 }
Greg Aker052b01d2011-04-21 14:38:03 -050066 }
67
Eric Barnes78ec0602011-11-27 00:48:56 -050068 // --------------------------------------------------------------------
69
Eric Barnes68286a42011-04-21 22:00:33 -040070 public function test_reduce_double_slashes()
Greg Aker052b01d2011-04-21 14:38:03 -050071 {
72 $strs = array(
73 'http://codeigniter.com' => 'http://codeigniter.com',
74 '//var/www/html/example.com/' => '/var/www/html/example.com/',
75 '/var/www/html//index.php' => '/var/www/html/index.php'
76 );
Eric Barnes78ec0602011-11-27 00:48:56 -050077
Greg Aker052b01d2011-04-21 14:38:03 -050078 foreach ($strs as $str => $expect)
79 {
80 $this->assertEquals($expect, reduce_double_slashes($str));
Eric Barnes78ec0602011-11-27 00:48:56 -050081 }
Greg Aker052b01d2011-04-21 14:38:03 -050082 }
83
Eric Barnes78ec0602011-11-27 00:48:56 -050084 // --------------------------------------------------------------------
85
Eric Barnes68286a42011-04-21 22:00:33 -040086 public function test_reduce_multiples()
Greg Aker052b01d2011-04-21 14:38:03 -050087 {
88 $strs = array(
89 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
90 'Ringo, John, Paul,,' => 'Ringo, John, Paul,'
91 );
Eric Barnes78ec0602011-11-27 00:48:56 -050092
Greg Aker052b01d2011-04-21 14:38:03 -050093 foreach ($strs as $str => $expect)
94 {
95 $this->assertEquals($expect, reduce_multiples($str));
96 }
Eric Barnes78ec0602011-11-27 00:48:56 -050097
Greg Aker052b01d2011-04-21 14:38:03 -050098 $strs = array(
99 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
100 'Ringo, John, Paul,,' => 'Ringo, John, Paul'
Eric Barnes78ec0602011-11-27 00:48:56 -0500101 );
Greg Aker052b01d2011-04-21 14:38:03 -0500102
103 foreach ($strs as $str => $expect)
104 {
105 $this->assertEquals($expect, reduce_multiples($str, ',', TRUE));
Eric Barnes78ec0602011-11-27 00:48:56 -0500106 }
Greg Aker052b01d2011-04-21 14:38:03 -0500107 }
Eric Barnes78ec0602011-11-27 00:48:56 -0500108
109 // --------------------------------------------------------------------
110
Eric Barnes68286a42011-04-21 22:00:33 -0400111 public function test_repeater()
Greg Aker052b01d2011-04-21 14:38:03 -0500112 {
113 $strs = array(
114 'a' => 'aaaaaaaaaa',
115 '&nbsp;' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
116 '<br>' => '<br><br><br><br><br><br><br><br><br><br>'
Eric Barnes78ec0602011-11-27 00:48:56 -0500117
Greg Aker052b01d2011-04-21 14:38:03 -0500118 );
Eric Barnes78ec0602011-11-27 00:48:56 -0500119
Greg Aker052b01d2011-04-21 14:38:03 -0500120 foreach ($strs as $str => $expect)
121 {
122 $this->assertEquals($expect, repeater($str, 10));
123 }
Eric Barnes78ec0602011-11-27 00:48:56 -0500124 }
Greg Aker052b01d2011-04-21 14:38:03 -0500125
Eric Barnes78ec0602011-11-27 00:48:56 -0500126 // --------------------------------------------------------------------
Eric Barnes68286a42011-04-21 22:00:33 -0400127
128 public function test_random_string()
129 {
130 $this->assertEquals(16, strlen(random_string('alnum', 16)));
131 $this->assertEquals(32, strlen(random_string('unique', 16)));
132 $this->assertInternalType('string', random_string('numeric', 16));
133 }
Eric Barnes78ec0602011-11-27 00:48:56 -0500134
135 // --------------------------------------------------------------------
136
137 public function test_increment_string()
138 {
139 $this->assertEquals('my-test_1', increment_string('my-test'));
140 $this->assertEquals('my-test-1', increment_string('my-test', '-'));
141 $this->assertEquals('file_5', increment_string('file_4'));
142 $this->assertEquals('file-5', increment_string('file-4', '-'));
143 $this->assertEquals('file-5', increment_string('file-4', '-'));
144 $this->assertEquals('file-1', increment_string('file', '-', '1'));
145 $this->assertEquals(124, increment_string('123', ''));
146 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300147
Greg Aker052b01d2011-04-21 14:38:03 -0500148}