WordPress.org

Make WordPress Core

Ticket #20124: smilies-unittests.diff

File smilies-unittests.diff, 18.7 KB (added by mdbitz, 8 years ago)

Enhanced Unit Tests to cover Tickets #16448, 20124, 25303

  • tests/phpunit/tests/formatting/Smilies.php

     
    55 */
    66class Tests_Formatting_Smilies extends WP_UnitTestCase {
    77
    8         function test_convert_smilies() {
    9                 global $wpsmiliestrans;
     8        /**
     9         * Basic Test Content DataProvider
     10         *
     11         * array ( input_txt, converted_output_txt)
     12         */
     13        public function get_smilies_input_output() {
    1014                $includes_path = includes_url("images/smilies/");
    1115
     16                return array (
     17                        array (
     18                                'Lorem ipsum dolor sit amet mauris ;-) Praesent gravida sodales. :lol: Vivamus nec diam in faucibus eu, bibendum varius nec, imperdiet purus est, at augue at lacus malesuada elit dapibus a, :eek: mauris. Cras mauris viverra elit. Nam laoreet viverra. Pellentesque tortor. Nam libero ante, porta urna ut turpis. Nullam wisi magna, :mrgreen: tincidunt nec, sagittis non, fringilla enim. Nam consectetuer nec, ullamcorper pede eu dui odio consequat vel, vehicula tortor quis pede turpis cursus quis, egestas ipsum ultricies ut, eleifend velit. Mauris vestibulum iaculis. Sed in nunc. Vivamus elit porttitor egestas. Mauris purus :?:',
     19                                'Lorem ipsum dolor sit amet mauris <img src=\''.$includes_path.'icon_wink.gif\' alt=\';-)\' class=\'wp-smiley\' />  Praesent gravida sodales. <img src=\''.$includes_path.'icon_lol.gif\' alt=\':lol:\' class=\'wp-smiley\' />  Vivamus nec diam in faucibus eu, bibendum varius nec, imperdiet purus est, at augue at lacus malesuada elit dapibus a, <img src=\''.$includes_path.'icon_surprised.gif\' alt=\':eek:\' class=\'wp-smiley\' />  mauris. Cras mauris viverra elit. Nam laoreet viverra. Pellentesque tortor. Nam libero ante, porta urna ut turpis. Nullam wisi magna, <img src=\''.$includes_path.'icon_mrgreen.gif\' alt=\':mrgreen:\' class=\'wp-smiley\' />  tincidunt nec, sagittis non, fringilla enim. Nam consectetuer nec, ullamcorper pede eu dui odio consequat vel, vehicula tortor quis pede turpis cursus quis, egestas ipsum ultricies ut, eleifend velit. Mauris vestibulum iaculis. Sed in nunc. Vivamus elit porttitor egestas. Mauris purus <img src=\''.$includes_path.'icon_question.gif\' alt=\':?:\' class=\'wp-smiley\' /> '
     20                        ),
     21                        array (
     22                                '<strong>Welcome to the jungle!</strong> We got fun n games! :) We got everything you want 8-) <em>Honey we know the names :)</em>',
     23                                '<strong>Welcome to the jungle!</strong> We got fun n games! <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' />  We got everything you want <img src=\''.$includes_path.'icon_cool.gif\' alt=\'8-)\' class=\'wp-smiley\' /> <em>Honey we know the names <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> </em>'
     24                        ),
     25                        array (
     26                                "<strong;)>a little bit of this\na little bit:other: of that :D\n:D a little bit of good\nyeah with a little bit of bad8O",
     27                                "<strong;)>a little bit of this\na little bit:other: of that <img src='{$includes_path}icon_biggrin.gif' alt=':D' class='wp-smiley' />  <img src='{$includes_path}icon_biggrin.gif' alt=':D' class='wp-smiley' />  a little bit of good\nyeah with a little bit of bad8O"
     28                        ),
     29                        array (
     30                                '<strong style="here comes the sun :-D">and I say it\'s allright:D:D',
     31                                '<strong style="here comes the sun :-D">and I say it\'s allright:D:D'
     32                        ),
     33                        array (
     34                                '<!-- Woo-hoo, I\'m a comment, baby! :x > -->',
     35                                '<!-- Woo-hoo, I\'m a comment, baby! :x > -->'
     36                        ),
     37                        array (
     38                                ':?:P:?::-x:mrgreen:::',
     39                                ' <img src=\''.$includes_path.'icon_question.gif\' alt=\':?:\' class=\'wp-smiley\' /> P:?::-x:mrgreen:::'
     40                        ),
     41                );
     42        }
     43
     44        /**
     45         * @dataProvider get_smilies_input_output
     46         *
     47         * Basic Validation Test to confirm that smilies are converted to image
     48         * when use_smilies = 1 and not when use_smilies = 0
     49         */
     50        function test_convert_standard_smilies( $in_txt, $converted_txt ) {
     51                global $wpsmiliestrans;
     52               
    1253                // standard smilies, use_smilies: ON
    1354                update_option( 'use_smilies', 1 );
    1455
    1556                smilies_init();
     57               
     58                $this->assertEquals( $converted_txt, convert_smilies($in_txt) );
    1659
    17                 $inputs = array(
    18                                                 'Lorem ipsum dolor sit amet mauris ;-) Praesent gravida sodales. :lol: Vivamus nec diam in faucibus eu, bibendum varius nec, imperdiet purus est, at augue at lacus malesuada elit dapibus a, :eek: mauris. Cras mauris viverra elit. Nam laoreet viverra. Pellentesque tortor. Nam libero ante, porta urna ut turpis. Nullam wisi magna, :mrgreen: tincidunt nec, sagittis non, fringilla enim. Nam consectetuer nec, ullamcorper pede eu dui odio consequat vel, vehicula tortor quis pede turpis cursus quis, egestas ipsum ultricies ut, eleifend velit. Mauris vestibulum iaculis. Sed in nunc. Vivamus elit porttitor egestas. Mauris purus :?:',
    19                                                 '<strong>Welcome to the jungle!</strong> We got fun n games! :) We got everything you want 8-) <em>Honey we know the names :)</em>',
    20                                                 "<strong;)>a little bit of this\na little bit:other: of that :D\n:D a little bit of good\nyeah with a little bit of bad8O",
    21                                                 '<strong style="here comes the sun :-D">and I say it\'s allright:D:D',
    22                                                 '<!-- Woo-hoo, I\'m a comment, baby! :x > -->',
    23                                                 ':?:P:?::-x:mrgreen:::', /*
    24                                                 'the question is, <textarea>Should smilies be converted in textareas :?:</textarea>',
    25                                                 'the question is, <code>Should smilies be converted in code or pre tags :?:</code>',
    26                                                 'the question is, <code style="color:#fff">Should smilies be converted in code or pre tags :?:</code>',
    27                                                 'the question is, <code>Should smilies be converted in invalid code or pre tags :?:</pre>',
    28                                                 '<Am I greedy?>Yes I am :)> :) The world makes me :mad:' */
    29                                                 );
    30 
    31                 $outputs = array(
    32                                                 'Lorem ipsum dolor sit amet mauris <img src=\''.$includes_path.'icon_wink.gif\' alt=\';-)\' class=\'wp-smiley\' />  Praesent gravida sodales. <img src=\''.$includes_path.'icon_lol.gif\' alt=\':lol:\' class=\'wp-smiley\' />  Vivamus nec diam in faucibus eu, bibendum varius nec, imperdiet purus est, at augue at lacus malesuada elit dapibus a, <img src=\''.$includes_path.'icon_surprised.gif\' alt=\':eek:\' class=\'wp-smiley\' />  mauris. Cras mauris viverra elit. Nam laoreet viverra. Pellentesque tortor. Nam libero ante, porta urna ut turpis. Nullam wisi magna, <img src=\''.$includes_path.'icon_mrgreen.gif\' alt=\':mrgreen:\' class=\'wp-smiley\' />  tincidunt nec, sagittis non, fringilla enim. Nam consectetuer nec, ullamcorper pede eu dui odio consequat vel, vehicula tortor quis pede turpis cursus quis, egestas ipsum ultricies ut, eleifend velit. Mauris vestibulum iaculis. Sed in nunc. Vivamus elit porttitor egestas. Mauris purus <img src=\''.$includes_path.'icon_question.gif\' alt=\':?:\' class=\'wp-smiley\' /> ',
    33                                                 '<strong>Welcome to the jungle!</strong> We got fun n games! <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' />  We got everything you want <img src=\''.$includes_path.'icon_cool.gif\' alt=\'8-)\' class=\'wp-smiley\' /> <em>Honey we know the names <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> </em>',
    34                                                 "<strong;)>a little bit of this\na little bit:other: of that <img src='{$includes_path}icon_biggrin.gif' alt=':D' class='wp-smiley' />  <img src='{$includes_path}icon_biggrin.gif' alt=':D' class='wp-smiley' />  a little bit of good\nyeah with a little bit of bad8O",
    35                                                 '<strong style="here comes the sun :-D">and I say it\'s allright:D:D',
    36                                                 '<!-- Woo-hoo, I\'m a comment, baby! :x > -->',
    37                                                 ' <img src=\''.$includes_path.'icon_question.gif\' alt=\':?:\' class=\'wp-smiley\' /> P:?::-x:mrgreen:::', /*
    38                                                 'the question is, <textarea>Should smilies be converted in textareas :?:</textarea>',
    39                                                 'the question is, <code>Should smilies be converted in code or pre tags :?:</code>',
    40                                                 'the question is, <code style="color:#fff">Should smilies be converted in code or pre tags :?:</code>',
    41                                                 'the question is, <code>Should smilies be converted in invalid code or pre tags :?:</pre>',
    42                                                 '<Am I greedy?>Yes I am <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> > <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' />  The world makes me <img src=\'http://wp-test.php/wp-includes/images/smilies/icon_mad.gif\' alt=\':mad:\' class=\'wp-smiley\' />' */
    43                                                 );
    44 
    45                 foreach ( $inputs as $k => $input ) {
    46                         $this->assertEquals( $outputs[$k], convert_smilies($input) );
    47                 }
    48 
     60                // standard smilies, use_smilies: OFF
    4961                update_option( 'use_smilies', 0 );
    5062
    51                 // standard smilies, use_smilies: OFF
     63                $this->assertEquals( $in_txt, convert_smilies($in_txt) );
     64               
     65                unset( $wpsmiliestrans );
     66        }
     67       
     68        /**
     69         * Custom Smilies Test Content DataProvider
     70         *
     71         * array ( input_txt, converted_output_txt)
     72         */
     73        public function get_custom_smilies_input_output() {
     74                $includes_path = includes_url("images/smilies/");
    5275
    53                 foreach ( $inputs as $input ) {
    54                         $this->assertEquals( $input, convert_smilies($input) );
    55                 }
    56 
    57                 return;
    58 
     76                return array (
     77                        array (
     78                                'Peter Brian Gabriel (born 13 February 1950) is a British singer, musician, and songwriter who rose to fame as the lead vocalist and flautist of the progressive rock group Genesis. :monkey:',
     79                                'Peter Brian Gabriel (born 13 February 1950) is a British singer, musician, and songwriter who rose to fame as the lead vocalist and flautist of the progressive rock group Genesis. <img src=\''.$includes_path.'icon_shock_the_monkey.gif\' alt=\':monkey:\' class=\'wp-smiley\' /> '
     80                        ),
     81                        array (
     82                                'Star Wars Jedi Knight :arrow: Jedi Academy is a first and third-person shooter action game set in the Star Wars universe. It was developed by Raven Software and published, distributed and marketed by LucasArts in North America and by Activision in the rest of the world. :nervou:',
     83                                'Star Wars Jedi Knight <img src=\''.$includes_path.'icon_arrow.gif\' alt=\':arrow:\' class=\'wp-smiley\' /> Jedi Academy is a first and third-person shooter action game set in the Star Wars universe. It was developed by Raven Software and published, distributed and marketed by LucasArts in North America and by Activision in the rest of the world. <img src=\''.$includes_path.'icon_nervou.gif\' alt=\':nervou:\' class=\'wp-smiley\' /> '
     84                        ),
     85                        array (
     86                                ':arrow: monkey: Lorem ipsum dolor sit amet enim. Etiam ullam :PP <br />corper. Suspendisse a pellentesque dui, non felis.<a> :arrow: :arrow</a>',
     87                                ' <img src=\''.$includes_path.'icon_arrow.gif\' alt=\':arrow:\' class=\'wp-smiley\' /> monkey: Lorem ipsum dolor sit amet enim. Etiam ullam <img src=\''.$includes_path.'icon_tongue.gif\' alt=\':PP\' class=\'wp-smiley\' /> <br />corper. Suspendisse a pellentesque dui, non felis.<a> <img src=\''.$includes_path.'icon_arrow.gif\' alt=\':arrow:\' class=\'wp-smiley\' /> :arrow</a>'
     88                        ),
     89                );
     90        }
     91       
     92        /**
     93         * @dataProvider get_custom_smilies_input_output
     94         *
     95         * Validate Custom Smilies are converted to images when use_smilies = 1
     96         * @uses $wpsmiliestrans
     97         */
     98        function test_convert_custom_smilies ( $in_txt, $converted_txt ) {
     99                global $wpsmiliestrans;
     100                $trans_orig = $wpsmiliestrans; // save original translations array
     101               
    59102                // custom smilies, use_smilies: ON
    60103                update_option( 'use_smilies', 1 );
    61104                $wpsmiliestrans = array(
     
    64107                  ':monkey:' => 'icon_shock_the_monkey.gif',
    65108                  ':nervou:' => 'icon_nervou.gif'
    66109                );
     110               
     111                smilies_init();
     112               
     113                $this->assertEquals( $converted_txt, convert_smilies($in_txt) );
    67114
     115                // standard smilies, use_smilies: OFF
     116                update_option( 'use_smilies', 0 );
     117
     118                $this->assertEquals( $in_txt, convert_smilies($in_txt) );
     119               
     120                $wpsmiliestrans = $trans_orig; // reset original translations array
     121        }
     122       
     123       
     124        /**
     125         * DataProvider of HTML elements/tags that smilie matches should be ignored in
     126         *
     127         */
     128        public function get_smilies_ignore_tags() {
     129                return array (
     130                        array( 'pre' ),
     131                        array( 'code' ),
     132                        array( 'script' ),
     133                        array( 'style' ),
     134                        array( 'textarea'),
     135                );
     136        }
     137       
     138        /**
     139         * Validate Conversion of Smilies is ignored in pre-determined tags
     140         * pre, code, script, style
     141         *
     142         * @ticket 16448
     143         * @dataProvider get_smilies_ignore_tags
     144         * @uses $wpsmiliestrans
     145         */
     146        public function test_ignore_smilies_in_tags( $element ) {
     147                global $wpsmiliestrans;
     148                $trans_orig = $wpsmiliestrans;  // save original translations array
     149               
     150                $includes_path = includes_url("images/smilies/");
     151               
     152                $in_str = 'Do we ingore smilies ;-) in ' . $element . ' tags <' . $element . '>My Content Here :?: </' . $element . '>';
     153                $exp_str = 'Do we ingore smilies <img src=\''.$includes_path.'icon_wink.gif\' alt=\';-)\' class=\'wp-smiley\' />  in ' . $element . ' tags <' . $element . '>My Content Here :?: </' . $element . '>';
     154               
     155                // standard smilies, use_smilies: ON
     156                update_option( 'use_smilies', 1 );
    68157                smilies_init();
     158               
     159                $this->assertEquals( $exp_str, convert_smilies($in_str) );
    69160
    70                 $inputs = array('Peter Brian Gabriel (born 13 February 1950) is a British singer, musician, and songwriter who rose to fame as the lead vocalist and flautist of the progressive rock group Genesis. :monkey:',
    71                                                 'Star Wars Jedi Knight:arrow: Jedi Academy is a first and third-person shooter action game set in the Star Wars universe. It was developed by Raven Software and published, distributed and marketed by LucasArts in North America and by Activision in the rest of the world. :nervou:',
    72                                                 ':arrow:monkey:Lorem ipsum dolor sit amet enim. Etiam ullam:PP<br />corper. Suspendisse a pellentesque dui, non felis.<a>:arrow::arrow</a>'
    73                                                 );
     161                // standard smilies, use_smilies: OFF
     162                update_option( 'use_smilies', 0 );
    74163
    75                 $outputs = array('Peter Brian Gabriel (born 13 February 1950) is a British singer, musician, and songwriter who rose to fame as the lead vocalist and flautist of the progressive rock group Genesis. <img src=\''.$includes_path.'icon_shock_the_monkey.gif\' alt=\'icon_arrow\' class=\'wp-smiley\' />',
    76                                                 'Star Wars Jedi Knight<img src=\''.$includes_path.'icon_arrow.gif\' alt=\'icon_arrow\' class=\'wp-smiley\' /> Jedi Academy is a first and third-person shooter action game set in the Star Wars universe. It was developed by Raven Software and published, distributed and marketed by LucasArts in North America and by Activision in the rest of the world. <img src=\''.$includes_path.'icon_nervou.gif\' alt=\'icon_nervou\' class=\'wp-smiley\' />',
    77                                                 '<img src=\''.$includes_path.'icon_arrow.gif\' alt=\'icon_arrow\' class=\'wp-smiley\' />monkey:Lorem ipsum dolor sit amet enim. Etiam ullam<img src=\''.$includes_path.'icon_tongue.gif\' alt=\'icon_tongue\' class=\'wp-smiley\' /><br />corper. Suspendisse a pellentesque dui, non felis.<a><img src=\''.$includes_path.'icon_arrow.gif\' alt=\'icon_arrow\' class=\'wp-smiley\' />:arrow</a>'
    78                                                 );
     164                $wpsmiliestrans = $trans_orig; // reset original translations array
     165        }
     166       
     167        /**
     168         * DataProvider of Smilie Combinations
     169         *
     170         */
     171        public function get_smilies_combinations() {
     172                $includes_path = includes_url("images/smilies/");
     173       
     174                return array (
     175                        array (
     176                                '8-O :-(',
     177                                ' <img src=\''.$includes_path.'icon_eek.gif\' alt=\'8-O\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_sad.gif\' alt=\':-(\' class=\'wp-smiley\' /> '
     178                        ),
     179                        array (
     180                                '8-) 8-O',
     181                                ' <img src=\''.$includes_path.'icon_cool.gif\' alt=\'8-)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_eek.gif\' alt=\'8-O\' class=\'wp-smiley\' /> '
     182                        ),
     183                        array (
     184                                '8-) 8O',
     185                                ' <img src=\''.$includes_path.'icon_cool.gif\' alt=\'8-)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_eek.gif\' alt=\'8O\' class=\'wp-smiley\' /> '
     186                        ),
     187                        array (
     188                                '8-) :-(',
     189                                ' <img src=\''.$includes_path.'icon_cool.gif\' alt=\'8-)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_sad.gif\' alt=\':-(\' class=\'wp-smiley\' /> '
     190                        ),
     191                        array (
     192                                '8-) :twisted:',
     193                                ' <img src=\''.$includes_path.'icon_cool.gif\' alt=\'8-)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_twisted.gif\' alt=\':twisted:\' class=\'wp-smiley\' /> '
     194                        ),
     195                        array (
     196                                '8O :twisted: :( :? :(',
     197                                ' <img src=\''.$includes_path.'icon_eek.gif\' alt=\'8O\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_twisted.gif\' alt=\':twisted:\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_sad.gif\' alt=\':(\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_confused.gif\' alt=\':?\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_sad.gif\' alt=\':(\' class=\'wp-smiley\' /> '
     198                        ),
     199                );
     200        }
     201       
     202        /**
     203         * Validate Combinations of Smilies separated by single space
     204         * are converted correctly
     205         *
     206         * @ticket 20124
     207         * @dataProvider get_smilies_combinations
     208         * @uses $wpsmiliestrans
     209         */
     210        public function test_smilies_combinations( $in_txt, $converted_txt ) {
     211                global $wpsmiliestrans;
     212               
     213                // custom smilies, use_smilies: ON
     214                update_option( 'use_smilies', 1 );
     215                smilies_init();
     216               
     217                $this->assertEquals( $converted_txt, convert_smilies($in_txt) );
    79218
    80                 foreach ( $inputs as $k => $input ) {
    81                         $this->assertEquals( $outputs[$k], convert_smilies($input) );
    82                 }
     219                // custom smilies, use_smilies: OFF
     220                update_option( 'use_smilies', 0 );
     221
     222                $this->assertEquals( $in_txt, convert_smilies($in_txt) );
    83223        }
     224       
     225        /**
     226         * DataProvider of Single Smilies input and converted output
     227         *
     228         */
     229        public function get_single_smilies_input_output() {
     230                $includes_path = includes_url("images/smilies/");
     231               
     232                return array (
     233                        array (
     234                                '8-O :-(',
     235                                '8-O :-('
     236                        ),
     237                        array (
     238                                '8O :) additional text here :)',
     239                                '8O <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> additional text here <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> '
     240                        ),
     241                        array (
     242                                ':) :) :) :)',
     243                                ' <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> <img src=\''.$includes_path.'icon_smile.gif\' alt=\':)\' class=\'wp-smiley\' /> '
     244                        ),
     245                );
     246        }
     247       
     248        /**
     249         * Validate Smilies are converted for single smilie in
     250         * the $wpsmiliestrans global array
     251         *
     252         * @ticket 25303
     253         * @dataProvider get_single_smilies_input_output
     254         * @uses $wpsmiliestrans
     255         */
     256        public function test_single_smilies_in_wpsmiliestrans( $in_txt, $converted_txt ) {
     257                global $wpsmiliestrans;
     258                $orig_trans = $wpsmiliestrans; // save original tranlations array
     259               
     260                // standard smilies, use_smilies: ON
     261                update_option( 'use_smilies', 1 );
     262               
     263                $wpsmiliestrans = array (
     264                  ':)' => 'icon_smile.gif'
     265                );
     266               
     267                smilies_init();
     268               
     269                $this->assertEquals( $converted_txt, convert_smilies($in_txt) );
     270
     271                // standard smilies, use_smilies: OFF
     272                update_option( 'use_smilies', 0 );
     273
     274                $this->assertEquals( $in_txt, convert_smilies($in_txt) );
     275               
     276                $wpsmiliestrans = $orig_trans; // reset original translations array
     277        }
     278       
    84279}