Make WordPress Core

Ticket #6464: test_smilies.php

File test_smilies.php, 5.5 KB (added by johanee, 17 years ago)

Test case for smilies, for use with Wordpress Test

Line 
1<?php
2
3// test convert_smilies and related functions
4
5
6/* Skip test? */
7function test_smilies_skip() {
8        global $wpsmiliestrans;
9
10        return ( count($wpsmiliestrans) == 0);
11}
12
13/* Parse text for image strings.
14 *
15 * Returns array of results with [0] as whole match including tag, [1] is src.
16 */
17function test_smilies_match_in_img($text) {
18        $match = "/<img.*src=(?:'|\")(.*)(?:'|\").*>/U";
19        preg_match_all($match, $text, $result, PREG_SET_ORDER);
20        return $result;
21}
22
23/*
24 * Create a random printable string of specified length, htmlentities-ed. More
25 * "aggressive" than rand_str().
26 */
27function test_smilies_random_string($length) {
28        $start_ascii_printable = 33;
29        $end_ascii_printable = 126;
30
31        $result = '';
32        while ($length--) {
33                $result .= chr(rand($start_ascii_printable, $end_ascii_printable));
34        }
35        return htmlentities($result);
36}
37
38/* Create a number of random words */
39function test_smilies_random_words($words, $strip_smilies=true, $min_length=3, $max_length=10) {
40        $result = ' ';
41        while($words--) {
42                $result .= test_smilies_random_string(rand($min_length, $max_length)) . ' ';
43        }
44        return ($strip_smilies ? test_smilies_strip_smilies($result) : $result);
45}
46
47/* Brute force strip smilies from string */
48function test_smilies_strip_smilies($text) {
49        global $wpsmiliestrans;
50        return str_replace(array_keys($wpsmiliestrans), '', $text);
51}
52
53/*
54 * Get specified number of random smilies from $wpsmiliestrans. Returns same
55 * format: <smilie> => <filename>
56 */
57function test_smilies_get_random_smilies($nr) {
58        global $wpsmiliestrans;
59
60        $keys = array_keys($wpsmiliestrans);
61        $values = array_values($wpsmiliestrans);
62
63        $count = count($keys);
64        while ($nr--) {
65                $i = rand(0, $count-1);
66                $result[$keys[$i]] = $values[$i];
67        }
68        return $result;
69}
70
71/* Get filename for smilie (as returned from test_smilies_match_in_img() */
72function test_smilies_get_filename($s) {
73        if (is_array($s)) {
74                $s = pathinfo($s[1]);
75                return $s['basename'];
76        } else {
77                return $s;
78        }
79}
80
81/*
82 * Convert $text using convert_smilies, and check that any smilies in converted
83 * text match one to one against array of smilie filenames supplied.
84 */
85function test_smilies_test_text($text, $smilies) {
86        $smilies_arr = test_smilies_match_in_img(convert_smilies($text));
87
88        if (count($smilies_arr) != count($smilies)) {
89                return FALSE;
90        }
91
92        /* make sure the filenames match */
93        $s = reset($smilies_arr);
94        $sfile = reset($smilies);
95        while ($s && $sfile) {
96                if (strcmp(test_smilies_get_filename($s), $sfile) != 0) {
97                        echo($s . ' / ' . test_smilies_get_filename($s) . '/' . $sfile);
98                        return FALSE;
99                }
100
101                $s = next($smilies_arr);
102                $sfile = next($smilies);
103        }
104
105        return TRUE;
106}
107
108
109class WPTestSmilies extends WPTestCase {
110        /*
111         * Test a few smilies among random text
112         */
113        function test_smilies_in_text() {
114                if (test_smilies_skip()) {
115                        $this->markTestSkipped();
116                }
117
118                $nr_of_smilies = 5;
119                $smilies = test_smilies_get_random_smilies($nr_of_smilies);
120
121                $text = '';
122                for ( $v = reset($smilies), $k = key($smilies);
123                          $v;
124                          $v = next($smilies), $k = key($smilies)) {
125                        $text .= test_smilies_random_words(rand(0, 15)) . $k . test_smilies_random_words(rand(0, 15));
126                }
127
128                $this->assertTrue(test_smilies_test_text($text, array_values($smilies)));
129        }
130
131        /*
132         * Test a smilie inside html tag
133         */
134        function test_smilies_in_tag() {
135                if (test_smilies_skip()) {
136                        $this->markTestSkipped();
137                }
138
139                $nr_of_smilies = 2;
140                $smilies = test_smilies_get_random_smilies($nr_of_smilies);
141
142                $text = '';
143                for ( $v = reset($smilies), $k = key($smilies);
144                          $v;
145                          $v = next($smilies), $k = key($smilies)) {
146                        $text .= '<label name="'. $k . '">';
147                }
148
149                // check against empty array
150                $this->assertTrue(test_smilies_test_text($text, array()));
151        }
152
153        /*
154         * Test a few smilies among html tags
155         */
156        function test_smilies_among_tags() {
157                if (test_smilies_skip()) {
158                        $this->markTestSkipped();
159                }
160
161                $nr_of_smilies = 5;
162                $smilies = test_smilies_get_random_smilies($nr_of_smilies);
163
164                $text = '';
165                for ( $v = reset($smilies), $k = key($smilies);
166                          $v;
167                          $v = next($smilies), $k = key($smilies)) {
168                        $text .=  '<p>' . $k . '</p>';
169                }
170
171                $this->assertTrue(test_smilies_test_text($text, array_values($smilies)));
172        }
173
174        /*
175         * Test one of each smilie
176         */
177        function test_all_smiles_and_filenames() {
178                if (test_smilies_skip()) {
179                        $this->markTestSkipped();
180                }
181
182                global $wpsmiliestrans;
183
184                /* make string with one of each smiley */
185                $smilies_text = '';
186                foreach (array_keys($wpsmiliestrans) as $smiley) {
187                        $smilies_text .= " $smiley ";
188                }
189
190                $this->assertTrue(test_smilies_test_text($smilies_text, array_values($wpsmiliestrans)));
191        }
192
193        /*
194         * Test a new $wpsmiliestrans with some random smilies
195         */
196        function test_smilies_new_wpsmiliestrans() {
197                /* This we can test even if test_smilies_skip() == true */
198
199                global $wpsmiliestrans;
200                $nr_of_smilies = 10;
201
202                /* backup */
203                $old_wpsmiliestrans = $wpsmiliestrans;
204                $old_option = get_option('use_smilies');
205                if (!$old_option) {
206                        update_option('use_smilies', 1);
207                }
208
209                unset($wpsmiliestrans);
210                while ($nr_of_smilies--) {
211                        $wpsmiliestrans[test_smilies_random_string(rand(2, 5))] = test_smilies_random_string(5, 10) . '.gif';
212                }
213
214                smilies_init();
215
216                $this->test_all_smiles_and_filenames();
217
218                /* restore */
219                $wpsmiliestrans = $old_wpsmiliestrans;
220                if (!$old_option) {
221                        update_option('use_smilies', $old_option);
222                }
223                smilies_init();
224        }
225}
226
227?>