| 1 | <?php |
| 2 | /** |
| 3 | * Test wp_load_alloptions(). |
| 4 | * |
| 5 | * @group option |
| 6 | */ |
| 7 | class Tests_Option_WP_Load_Alloptions extends WP_UnitTestCase { |
| 8 | protected $alloptions = null; |
| 9 | |
| 10 | function tearDown() { |
| 11 | $this->alloptions = null; |
| 12 | parent::tearDown(); |
| 13 | } |
| 14 | |
| 15 | function test_if_alloptions_is_cached() { |
| 16 | $this->assertNotEmpty( wp_cache_get( 'alloptions', 'options' ) ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @depends test_if_alloptions_is_cached |
| 21 | */ |
| 22 | function test_if_cached_alloptions_is_deleted() { |
| 23 | $this->assertTrue( wp_cache_delete( 'alloptions', 'options' ) ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @depends test_if_alloptions_is_cached |
| 28 | */ |
| 29 | function test_if_alloptions_are_retrieved_from_cache() { |
| 30 | global $wpdb; |
| 31 | $before = $wpdb->num_queries; |
| 32 | wp_load_alloptions(); |
| 33 | $after = $wpdb->num_queries; |
| 34 | |
| 35 | // Database has not been hit. |
| 36 | $this->assertEquals( $before, $after ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @depends test_if_cached_alloptions_is_deleted |
| 41 | */ |
| 42 | function test_if_alloptions_are_retrieved_from_database() { |
| 43 | global $wpdb; |
| 44 | |
| 45 | // Delete the existing cache first. |
| 46 | wp_cache_delete( 'alloptions', 'options' ); |
| 47 | |
| 48 | $before = $wpdb->num_queries; |
| 49 | wp_load_alloptions(); |
| 50 | $after = $wpdb->num_queries; |
| 51 | |
| 52 | // Database has been hit. |
| 53 | $this->assertEquals( $before + 1, $after ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @depends test_if_cached_alloptions_is_deleted |
| 58 | */ |
| 59 | function test_filter_pre_cache_alloptions_is_called() { |
| 60 | $temp = wp_installing(); |
| 61 | |
| 62 | /** |
| 63 | * Set wp_installing() to false. |
| 64 | * |
| 65 | * If wp_installing is false and the cache is empty, the filter is called regardless if it's multisite or not. |
| 66 | */ |
| 67 | wp_installing( false ); |
| 68 | |
| 69 | // Delete the existing cache first. |
| 70 | wp_cache_delete( 'alloptions', 'options' ); |
| 71 | |
| 72 | add_filter( 'pre_cache_alloptions', array( $this, 'return_pre_cache_filter' ) ); |
| 73 | $all_options = wp_load_alloptions(); |
| 74 | |
| 75 | // Value could leak to other tests if not reset. |
| 76 | wp_installing( $temp ); |
| 77 | |
| 78 | // Filter was called. |
| 79 | $this->assertEquals( $this->alloptions, $all_options ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @depends test_if_alloptions_is_cached |
| 84 | */ |
| 85 | function test_filter_pre_cache_alloptions_is_not_called() { |
| 86 | $temp = wp_installing(); |
| 87 | |
| 88 | /** |
| 89 | * Set wp_installing() to true. |
| 90 | * |
| 91 | * If wp_installing is true and it's multisite, the cache and filter are not used. |
| 92 | * If wp_installing is true and it's not multisite, the cache is used (if not empty), and the filter not. |
| 93 | */ |
| 94 | wp_installing( true ); |
| 95 | |
| 96 | add_filter( 'pre_cache_alloptions', array( $this, 'return_pre_cache_filter' ) ); |
| 97 | wp_load_alloptions(); |
| 98 | |
| 99 | // Value could leak to other tests if not reset. |
| 100 | wp_installing( $temp ); |
| 101 | |
| 102 | // Filter was not called. |
| 103 | $this->assertNull( $this->alloptions ); |
| 104 | } |
| 105 | |
| 106 | function return_pre_cache_filter( $alloptions ) { |
| 107 | return $this->alloptions = $alloptions; |
| 108 | } |
| 109 | } |