Make WordPress Core

Ticket #35379: 35379_unit_test.diff

File 35379_unit_test.diff, 2.8 KB (added by codex-m, 9 years ago)

Unit test

  • tests/phpunit/tests/option/networkOption.php

     
    8282                        array( 'string', false ),
    8383                );
    8484        }
     85       
     86        /**
     87         * @ticket 35379
     88         */
     89        function test_update_network_option_stored_values() {
     90       
     91                //Switch to main site
     92                switch_to_blog(1);
     93       
     94                $id = self::factory()->network->create();
     95       
     96                /**
     97                 * Three default options known to exists in both main site options table and
     98                 * And network options table:
     99                 * admin_email
     100                 * siteurl
     101                 * WPLANG
     102                 * If wrong values are inputted to these options using update_network_option
     103                 * Then the original stored values should remain unchanged and not using the main site option values
     104                 *
     105                 */
     106       
     107                $affected_default_options=array(
     108                                'admin_email'=> array(
     109                                                'main_site'=>'admin@example.org',
     110                                                'network'  =>'network_admin@example.org',
     111                                                'incorrect'=>rand_str()
     112                                ),
     113                                'siteurl'        =>array(
     114                                                'main_site'=>'http://example.org/src/',
     115                                                'network'  =>'http://example.org/',
     116                                                'incorrect'=>rand_str()
     117                                ),
     118                                'WPLANG'         =>array(
     119                                                'main_site'=>'en_CA',
     120                                                'network'  =>'en_US',
     121                                                'incorrect'=>rand_str()
     122                                )
     123                );
     124       
     125                $failures=array();
     126                foreach ($affected_default_options as $option_name=>$affected_default_option) {
     127       
     128                        $option_value_main_site=$affected_default_option['main_site'];
     129                        $option_value_network= $affected_default_option['network'];
     130                        $incorrect_value_for_testing= $affected_default_option['incorrect'];
     131       
     132                        //Update main site option value
     133                        update_option($option_name,$option_value_main_site);
     134       
     135                        //Update network site option value
     136                        update_network_option($id ,$option_name,$option_value_network);
     137       
     138                        //Check if they are set correctly (debugging purposes)
     139                        $set_main_site_option_retrieved_correct= get_option($option_name);
     140                        $set_network_option_retrieved_correct= get_network_option($id ,$option_name);
     141       
     142                        //Set an invalid value on update_network_option
     143                        update_network_option( $id , $option_name, $incorrect_value_for_testing );
     144       
     145                        //Retrieve again the value of network admin email
     146                        $new_network_value_retrieved= get_network_option( $id , $option_name);
     147       
     148                        //Expected value is that the network admin email should be unchanged if submitted admin email is invalid
     149                        if ( $new_network_value_retrieved != $option_value_network) {
     150                                $failures[]=$option_name;
     151                        }
     152       
     153                }
     154       
     155                $count=count($affected_default_options);
     156                $failure_count=count($failures);
     157       
     158                $message='';
     159                if ($failure_count > 0) {
     160                        $comma_separated= implode(',',$failures);
     161                        $message= $comma_separated.' fails validation.';
     162                }
     163                $this->assertEquals( 0, $failure_count,$message );
     164        }       
    85165}
    86166
    87167endif;
     168 No newline at end of file