|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test register_default_headers() and unregister_default_headers(). |
| 4 | + * |
| 5 | + * @group themes |
| 6 | + */ |
| 7 | +class Tests_Theme_DefaultHeaders extends WP_UnitTestCase { |
| 8 | + |
| 9 | + /** |
| 10 | + * Backup of $_wp_default_headers. |
| 11 | + * |
| 12 | + * @var array|null |
| 13 | + */ |
| 14 | + protected $old_wp_default_headers; |
| 15 | + |
| 16 | + /** |
| 17 | + * Set up before each test. |
| 18 | + */ |
| 19 | + public function set_up() { |
| 20 | + parent::set_up(); |
| 21 | + global $_wp_default_headers; |
| 22 | + $this->old_wp_default_headers = $_wp_default_headers; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Clean up after each test. |
| 27 | + */ |
| 28 | + public function tear_down() { |
| 29 | + global $_wp_default_headers; |
| 30 | + $_wp_default_headers = $this->old_wp_default_headers; |
| 31 | + parent::tear_down(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Tests that register_default_headers() adds headers and merges subsequent registrations without replacement. |
| 36 | + * |
| 37 | + * @ticket 21876 |
| 38 | + * |
| 39 | + * @covers ::register_default_headers |
| 40 | + */ |
| 41 | + public function test_register_default_headers() { |
| 42 | + global $_wp_default_headers; |
| 43 | + |
| 44 | + $first_batch = array( |
| 45 | + 'first_batch_header' => array( |
| 46 | + 'url' => '%s/images/headers/first.jpg', |
| 47 | + 'thumbnail_url' => '%s/images/headers/first-thumb.jpg', |
| 48 | + 'description' => 'First Batch Header', |
| 49 | + ), |
| 50 | + ); |
| 51 | + |
| 52 | + $second_batch = array( |
| 53 | + 'second_batch_header' => array( |
| 54 | + 'url' => '%s/images/headers/second.jpg', |
| 55 | + 'thumbnail_url' => '%s/images/headers/second-thumb.jpg', |
| 56 | + 'description' => 'Second Batch Header', |
| 57 | + ), |
| 58 | + ); |
| 59 | + |
| 60 | + register_default_headers( $first_batch ); |
| 61 | + register_default_headers( $second_batch ); |
| 62 | + |
| 63 | + $this->assertIsArray( $_wp_default_headers, 'Global $_wp_default_headers should be an array.' ); |
| 64 | + $this->assertArrayHasKey( 'first_batch_header', $_wp_default_headers, 'First batch header should remain present after merge.' ); |
| 65 | + $this->assertSame( $first_batch['first_batch_header'], $_wp_default_headers['first_batch_header'], 'First batch header metadata should match.' ); |
| 66 | + $this->assertArrayHasKey( 'second_batch_header', $_wp_default_headers, 'Second batch header should be present after merge.' ); |
| 67 | + $this->assertSame( $second_batch['second_batch_header'], $_wp_default_headers['second_batch_header'], 'Second batch header metadata should match.' ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Tests that unregister_default_headers() removes a single header and returns true on success. |
| 72 | + * |
| 73 | + * @ticket 21876 |
| 74 | + * |
| 75 | + * @covers ::unregister_default_headers |
| 76 | + */ |
| 77 | + public function test_unregister_default_headers_single_success() { |
| 78 | + global $_wp_default_headers; |
| 79 | + |
| 80 | + $header_id = 'test_header_to_remove'; |
| 81 | + register_default_headers( |
| 82 | + array( |
| 83 | + $header_id => array( |
| 84 | + 'url' => '%s/images/headers/remove.jpg', |
| 85 | + 'thumbnail_url' => '%s/images/headers/remove-thumb.jpg', |
| 86 | + 'description' => 'Header To Remove', |
| 87 | + ), |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + $result = unregister_default_headers( $header_id ); |
| 92 | + |
| 93 | + $this->assertTrue( $result, 'unregister_default_headers() should return true when single header is removed.' ); |
| 94 | + $this->assertArrayNotHasKey( $header_id, $_wp_default_headers, 'Header should no longer exist in global array.' ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Tests that unregister_default_headers() returns false when removing a non-existent header. |
| 99 | + * |
| 100 | + * @ticket 21876 |
| 101 | + * |
| 102 | + * @covers ::unregister_default_headers |
| 103 | + */ |
| 104 | + public function test_unregister_default_headers_single_failure() { |
| 105 | + $result = unregister_default_headers( 'non_existent_header_id_12345' ); |
| 106 | + |
| 107 | + $this->assertFalse( $result, 'unregister_default_headers() should return false when header does not exist.' ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Tests that unregister_default_headers() removes multiple headers when passed an array and returns null. |
| 112 | + * |
| 113 | + * @ticket 21876 |
| 114 | + * |
| 115 | + * @covers ::unregister_default_headers |
| 116 | + */ |
| 117 | + public function test_unregister_default_headers_array() { |
| 118 | + global $_wp_default_headers; |
| 119 | + |
| 120 | + $headers = array( |
| 121 | + 'batch_remove_one' => array( |
| 122 | + 'url' => '%s/images/headers/one.jpg', |
| 123 | + 'thumbnail_url' => '%s/images/headers/one-thumb.jpg', |
| 124 | + 'description' => 'Batch One', |
| 125 | + ), |
| 126 | + 'batch_remove_two' => array( |
| 127 | + 'url' => '%s/images/headers/two.jpg', |
| 128 | + 'thumbnail_url' => '%s/images/headers/two-thumb.jpg', |
| 129 | + 'description' => 'Batch Two', |
| 130 | + ), |
| 131 | + ); |
| 132 | + |
| 133 | + register_default_headers( $headers ); |
| 134 | + |
| 135 | + $result = unregister_default_headers( array( 'batch_remove_one', 'batch_remove_two' ) ); |
| 136 | + |
| 137 | + $this->assertNull( $result, 'unregister_default_headers() should return null when given an array of headers.' ); |
| 138 | + $this->assertArrayNotHasKey( 'batch_remove_one', $_wp_default_headers, 'First header should be removed.' ); |
| 139 | + $this->assertArrayNotHasKey( 'batch_remove_two', $_wp_default_headers, 'Second header should be removed.' ); |
| 140 | + } |
| 141 | +} |
0 commit comments