Make WordPress Core

source: trunk/tests/phpunit/tests/option/wpSetOptionAutoload.php @ 56508

Last change on this file since 56508 was 56508, checked in by flixos90, 11 months ago

Options, Meta APIs: Introduce wp_set_option_autoload_values().

This function accepts an associative array of option names and their autoload values to set, and it will update those values in the database in bulk, only for those options where the autoload field is not already set to the given value.

Two wrapper functions for ease of use accompany the new main function:

  • wp_set_options_autoload( $options, $autoload ) can be used to set multiple options to the same autoload value.
  • wp_set_option_autoload( $option, $autoload ) can be used to set the autoload value for a single option.

All of these functions allow changing the autoload value of an option, which previously has only been possible in combination with updating the value. This limitation prevented some relevant use-cases: For example, a plugin deactivation hook could set all of its options to not autoload, as a cleanup routine, while not actually deleting any data. The plugin's activation hook could then implement the reverse, resetting those options' autoload values to the originally intended ones for when using the plugin.

Props boonebgorges, joemcgill, costdev, mukesh27, SergeyBiryukov, tabrisrp, flixos90.
Fixes #58964.

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1<?php
2/**
3 * Test wp_set_option_autoload().
4 *
5 * @group option
6 *
7 * @covers ::wp_set_option_autoload
8 */
9class Tests_Option_WpSetOptionAutoload extends WP_UnitTestCase {
10
11        /**
12         * Tests that setting an option's autoload value to 'yes' works as expected.
13         *
14         * @ticket 58964
15         */
16        public function test_wp_set_option_autoload_yes() {
17                global $wpdb;
18
19                $option = 'test_option';
20                $value  = 'value';
21
22                add_option( $option, $value, '', 'no' );
23
24                $this->assertTrue( wp_set_option_autoload( $option, 'yes' ), 'Function did not succeed' );
25                $this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
26                $this->assertFalse( wp_cache_get( $option, 'options' ), 'Option not deleted from individual cache' );
27                $this->assertFalse( wp_cache_get( 'alloptions', 'options' ), 'Alloptions cache not cleared' );
28        }
29
30        /**
31         * Tests that setting an option's autoload value to 'no' works as expected.
32         *
33         * @ticket 58964
34         */
35        public function test_wp_set_option_autoload_no() {
36                global $wpdb;
37
38                $option = 'test_option';
39                $value  = 'value';
40
41                add_option( $option, $value, '', 'yes' );
42
43                $this->assertTrue( wp_set_option_autoload( $option, 'no' ), 'Function did not succeed' );
44                $this->assertSame( 'no', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
45                $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), 'Option not deleted from alloptions cache' );
46        }
47
48        /**
49         * Tests that setting an option's autoload value to the same value as prior works as expected.
50         *
51         * @ticket 58964
52         */
53        public function test_wp_set_option_autoload_same() {
54                global $wpdb;
55
56                $option = 'test_option';
57                $value  = 'value';
58
59                add_option( $option, $value, '', 'yes' );
60
61                $this->assertFalse( wp_set_option_autoload( $option, 'yes' ), 'Function did unexpectedly succeed' );
62                $this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value unexpectedly updated in database' );
63        }
64
65        /**
66         * Tests that setting a missing option's autoload value does not do anything.
67         *
68         * @ticket 58964
69         */
70        public function test_wp_set_option_autoload_missing() {
71                global $wpdb;
72
73                $option = 'test_option';
74
75                $this->assertFalse( wp_set_option_autoload( $option, 'yes' ), 'Function did unexpectedly succeed' );
76                $this->assertNull( $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Missing option autoload value was set in database' );
77                $this->assertArrayNotHasKey( $option, wp_cache_get( 'alloptions', 'options' ), 'Missing option found in alloptions cache' );
78                $this->assertFalse( wp_cache_get( $option, 'options' ), 'Missing option found in individual cache' );
79        }
80
81        /**
82         * Tests setting an option's autoload value to boolean true.
83         *
84         * @ticket 58964
85         */
86        public function test_wp_set_option_autoload_true() {
87                global $wpdb;
88
89                $option = 'test_option';
90                $value  = 'value';
91
92                add_option( $option, $value, '', false );
93
94                $this->assertTrue( wp_set_option_autoload( $option, true ), 'Function did not succeed' );
95                $this->assertSame( 'yes', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
96        }
97
98        /**
99         * Tests setting an option's autoload value to boolean false.
100         *
101         * @ticket 58964
102         */
103        public function test_wp_set_option_autoload_false() {
104                global $wpdb;
105
106                $option = 'test_option';
107                $value  = 'value';
108
109                add_option( $option, $value, '', true );
110
111                $this->assertTrue( wp_set_option_autoload( $option, false ), 'Function did not succeed' );
112                $this->assertSame( 'no', $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ), 'Option autoload value not updated in database' );
113        }
114}
Note: See TracBrowser for help on using the repository browser.