|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for WP_Customize_Post_Terms_Setting. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Customize |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Class Test_Customize_Post_Terms_Setting |
| 11 | + * |
| 12 | + * @covers WP_Customize_Post_Terms_Setting |
| 13 | + */ |
| 14 | +class Test_Customize_Post_Terms_Setting extends WP_UnitTestCase { |
| 15 | + |
| 16 | + /** |
| 17 | + * Plugin. |
| 18 | + * |
| 19 | + * @var Customize_Posts_Plugin |
| 20 | + */ |
| 21 | + public $plugin; |
| 22 | + |
| 23 | + /** |
| 24 | + * Manager. |
| 25 | + * |
| 26 | + * @var WP_Customize_Manager |
| 27 | + */ |
| 28 | + public $manager; |
| 29 | + |
| 30 | + /** |
| 31 | + * Set up. |
| 32 | + */ |
| 33 | + function setUp() { |
| 34 | + parent::setUp(); |
| 35 | + remove_all_filters( 'customize_loaded_components' ); |
| 36 | + wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) ); |
| 37 | + require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); |
| 38 | + $this->plugin = new Customize_Posts_Plugin(); |
| 39 | + // @codingStandardsIgnoreStart |
| 40 | + $GLOBALS['wp_customize'] = new WP_Customize_Manager(); |
| 41 | + // @codingStandardsIgnoreStop |
| 42 | + $this->manager = $GLOBALS['wp_customize']; |
| 43 | + |
| 44 | + remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); |
| 45 | + remove_action( 'after_setup_theme', 'twentysixteen_setup' ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Tear down. |
| 50 | + */ |
| 51 | + function tearDown() { |
| 52 | + unset( $GLOBALS['wp_customize'] ); |
| 53 | + parent::tearDown(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test get_post_terms_setting_id() method. |
| 58 | + * |
| 59 | + * @covers WP_Customize_Post_Terms_Setting::get_post_terms_setting_id() |
| 60 | + */ |
| 61 | + function test_get_post_terms_setting_id() { |
| 62 | + $post = get_post( $this->factory()->post->create( array( 'post_type' => 'page' ) ) ); |
| 63 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( $post, 'category' ); |
| 64 | + $this->assertEquals( "post_terms[page][$post->ID][category]", $setting_id ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test constructor exceptions. |
| 69 | + * |
| 70 | + * @covers WP_Customize_Post_Terms_Setting::__construct() |
| 71 | + */ |
| 72 | + function test_construct_exceptions() { |
| 73 | + // Test illegal setting id. |
| 74 | + $exception = null; |
| 75 | + try { |
| 76 | + new WP_Customize_Post_Terms_Setting( $this->manager, 'bad' ); |
| 77 | + } catch ( Exception $e ) { |
| 78 | + $exception = $e; |
| 79 | + } |
| 80 | + $this->assertInstanceOf( 'Exception', $exception ); |
| 81 | + $this->assertContains( 'Illegal setting id', $exception->getMessage() ); |
| 82 | + |
| 83 | + // Test illegal setting id. |
| 84 | + $exception = null; |
| 85 | + try { |
| 86 | + new WP_Customize_Post_Terms_Setting( $this->manager, sprintf( 'post_terms[post][%d][food]', -123 ) ); |
| 87 | + } catch ( Exception $e ) { |
| 88 | + $exception = $e; |
| 89 | + } |
| 90 | + $this->assertInstanceOf( 'Exception', $exception ); |
| 91 | + $this->assertContains( 'Illegal setting id', $exception->getMessage() ); |
| 92 | + |
| 93 | + // Test unrecognized post type. |
| 94 | + $bad_post_id = $this->factory()->post->create( array( 'post_type' => 'unknown' ) ); |
| 95 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $bad_post_id ), 'bad' ); |
| 96 | + try { |
| 97 | + new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 98 | + } catch ( Exception $e ) { |
| 99 | + $exception = $e; |
| 100 | + } |
| 101 | + $this->assertInstanceOf( 'Exception', $exception ); |
| 102 | + $this->assertContains( 'Unrecognized post type', $exception->getMessage() ); |
| 103 | + |
| 104 | + // Test unrecognized taxonomy. |
| 105 | + $post_id = $this->factory()->post->create( array( 'post_type' => 'post' ) ); |
| 106 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $post_id ), 'bad' ); |
| 107 | + try { |
| 108 | + new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 109 | + } catch ( Exception $e ) { |
| 110 | + $exception = $e; |
| 111 | + } |
| 112 | + $this->assertInstanceOf( 'Exception', $exception ); |
| 113 | + $this->assertContains( 'Unrecognized taxonomy', $exception->getMessage() ); |
| 114 | + |
| 115 | + // Test posts component is not created. |
| 116 | + unset( $this->manager->posts ); |
| 117 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $post_id ), 'category' ); |
| 118 | + try { |
| 119 | + new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 120 | + } catch ( Exception $e ) { |
| 121 | + $exception = $e; |
| 122 | + } |
| 123 | + $this->manager->posts = $this->posts; |
| 124 | + $this->assertInstanceOf( 'Exception', $exception ); |
| 125 | + $this->assertContains( 'Posts component not instantiated', $exception->getMessage() ); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test constructor properties. |
| 130 | + * |
| 131 | + * @covers WP_Customize_Post_Terms_Setting::__construct() |
| 132 | + */ |
| 133 | + function test_construct_properties() { |
| 134 | + $admin_user_id = get_current_user_id(); |
| 135 | + $this->assertTrue( current_user_can( 'manage_categories' ) ); |
| 136 | + $post_id = $this->factory()->post->create( array( 'post_type' => 'post', 'post_author' => $admin_user_id ) ); |
| 137 | + $post = get_post( $post_id ); |
| 138 | + $taxonomy = 'category'; |
| 139 | + $setting_id = sprintf( 'post_terms[post][%d][%s]', $post_id, $taxonomy ); |
| 140 | + |
| 141 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 142 | + $this->assertEquals( $post_id, $setting->post_id ); |
| 143 | + $this->assertEquals( $post->post_type, $setting->post_type ); |
| 144 | + $this->assertEquals( $taxonomy, $setting->taxonomy ); |
| 145 | + $this->assertEquals( array(), $setting->default ); |
| 146 | + $this->assertEquals( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.7', '>=' ) ? 'assign_categories' : 'edit_posts', $setting->capability ); |
| 147 | + $this->assertInstanceOf( 'WP_Customize_Posts', $setting->posts_component ); |
| 148 | + |
| 149 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id, array( |
| 150 | + 'capability' => 'create_awesome', |
| 151 | + ) ); |
| 152 | + $this->assertEquals( 'create_awesome', $setting->capability ); |
| 153 | + $this->assertEquals( array(), $setting->default ); |
| 154 | + |
| 155 | + add_filter( 'user_has_cap', '__return_empty_array' ); |
| 156 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id, array( |
| 157 | + 'capability' => 'delete_awesome', |
| 158 | + ) ); |
| 159 | + $this->assertEquals( 'delete_awesome', $setting->capability ); |
| 160 | + remove_filter( 'user_has_cap', '__return_empty_array' ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Test sanitize. |
| 165 | + * |
| 166 | + * @covers WP_Customize_Post_Terms_Setting::sanitize() |
| 167 | + */ |
| 168 | + function test_sanitize() { |
| 169 | + $post_id = $this->factory()->post->create( array( 'post_type' => 'post') ); |
| 170 | + $taxonomy = 'category'; |
| 171 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $post_id ), $taxonomy ); |
| 172 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 173 | + $term_id = $this->factory()->term->create( array( |
| 174 | + 'taxonomy' => $taxonomy, |
| 175 | + 'name' => 'Foo', |
| 176 | + ) ); |
| 177 | + $this->assertSame( $setting->sanitize( array( (string) $term_id ) ), array( $term_id ) ); |
| 178 | + |
| 179 | + $this->assertSame( $setting->sanitize( array() ), array() ); |
| 180 | + |
| 181 | + $validity = $setting->sanitize( 'bad' ); |
| 182 | + $this->assertInstanceOf( 'WP_Error', $validity ); |
| 183 | + $this->assertEquals( 'expected_array', $validity->get_error_code() ); |
| 184 | + |
| 185 | + $validity = $setting->sanitize( array( 'bad' ) ); |
| 186 | + $this->assertInstanceOf( 'WP_Error', $validity ); |
| 187 | + $this->assertEquals( 'invalid_term_id', $validity->get_error_code() ); |
| 188 | + |
| 189 | + $validity = $setting->sanitize( array( -1 ) ); |
| 190 | + $this->assertInstanceOf( 'WP_Error', $validity ); |
| 191 | + $this->assertEquals( 'invalid_term_id', $validity->get_error_code() ); |
| 192 | + |
| 193 | + $tag_term_id = $this->factory()->term->create( array( |
| 194 | + 'taxonomy' => 'post_tag', |
| 195 | + 'name' => 'Foo', |
| 196 | + ) ); |
| 197 | + $validity = $setting->sanitize( array( $tag_term_id ) ); |
| 198 | + $this->assertInstanceOf( 'WP_Error', $validity ); |
| 199 | + $this->assertEquals( 'missing_term', $validity->get_error_code() ); |
| 200 | + |
| 201 | + add_filter( "customize_sanitize_{$setting_id}", '__return_empty_array' ); |
| 202 | + $this->assertSame( $setting->sanitize( array( 1, 2, 3 ) ), array() ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Test getting value that is not previewed. |
| 207 | + * |
| 208 | + * @covers WP_Customize_Post_Terms_Setting::value() |
| 209 | + */ |
| 210 | + function test_non_previewed_value() { |
| 211 | + $post_id = $this->factory()->post->create( array( 'post_type' => 'post') ); |
| 212 | + |
| 213 | + $taxonomy = 'post_tag'; |
| 214 | + $term_id_1 = $this->factory()->term->create( array( |
| 215 | + 'taxonomy' => $taxonomy, |
| 216 | + 'name' => 'Foo', |
| 217 | + ) ); |
| 218 | + $term_id_2 = $this->factory()->term->create( array( |
| 219 | + 'taxonomy' => $taxonomy, |
| 220 | + 'name' => 'Bar', |
| 221 | + ) ); |
| 222 | + wp_set_post_terms( $post_id, array( $term_id_1 ), $taxonomy ); |
| 223 | + |
| 224 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $post_id ), $taxonomy ); |
| 225 | + $exception = null; |
| 226 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 227 | + $this->assertSame( array( $term_id_1 ), $setting->value() ); |
| 228 | + wp_set_post_terms( $post_id, array( $term_id_2 ), $taxonomy ); |
| 229 | + $this->assertSame( array( $term_id_2 ), $setting->value() ); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Test previewing. |
| 234 | + * |
| 235 | + * @covers WP_Customize_Post_Terms_Setting::preview() |
| 236 | + */ |
| 237 | + function test_preview() { |
| 238 | + $post_id = $this->factory()->post->create( array( 'post_type' => 'post') ); |
| 239 | + |
| 240 | + $taxonomy = 'post_tag'; |
| 241 | + $term_id_1 = $this->factory()->term->create( array( |
| 242 | + 'taxonomy' => $taxonomy, |
| 243 | + 'name' => 'Foo', |
| 244 | + ) ); |
| 245 | + $term_id_2 = $this->factory()->term->create( array( |
| 246 | + 'taxonomy' => $taxonomy, |
| 247 | + 'name' => 'Bar', |
| 248 | + ) ); |
| 249 | + $initial_post_terms = array( $term_id_1 ); |
| 250 | + wp_set_post_terms( $post_id, $initial_post_terms, $taxonomy ); |
| 251 | + |
| 252 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $post_id ), $taxonomy ); |
| 253 | + $previewed_post_terms = array( $term_id_2 ); |
| 254 | + $this->manager->set_post_value( $setting_id, $previewed_post_terms ); |
| 255 | + |
| 256 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 257 | + $this->assertEquals( $initial_post_terms, $setting->value() ); |
| 258 | + $this->assertEquals( $initial_post_terms, wp_list_pluck( get_the_terms( $post_id, $taxonomy ), 'term_id' ) ); |
| 259 | + $this->assertTrue( $setting->preview() ); |
| 260 | + $this->assertEquals( $previewed_post_terms, $setting->value() ); |
| 261 | + $this->assertEquals( $previewed_post_terms, wp_list_pluck( get_the_terms( $post_id, $taxonomy ), 'term_id' ) ); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Test single save. |
| 266 | + * |
| 267 | + * @covers WP_Customize_Post_Terms_Setting::update() |
| 268 | + */ |
| 269 | + function test_update() { |
| 270 | + $post_id = $this->factory()->post->create( array( 'post_type' => 'post') ); |
| 271 | + $taxonomy = 'post_tag'; |
| 272 | + $term_id_1 = $this->factory()->term->create( array( |
| 273 | + 'taxonomy' => $taxonomy, |
| 274 | + 'name' => 'Foo', |
| 275 | + ) ); |
| 276 | + $term_id_2 = $this->factory()->term->create( array( |
| 277 | + 'taxonomy' => $taxonomy, |
| 278 | + 'name' => 'Bar', |
| 279 | + ) ); |
| 280 | + $initial_post_terms = array( $term_id_1 ); |
| 281 | + wp_set_post_terms( $post_id, $initial_post_terms, $taxonomy ); |
| 282 | + |
| 283 | + $setting_id = WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $post_id ), $taxonomy ); |
| 284 | + $modified_post_terms = array( $term_id_2 ); |
| 285 | + $this->manager->set_post_value( $setting_id, $modified_post_terms ); |
| 286 | + |
| 287 | + $setting = new WP_Customize_Post_Terms_Setting( $this->manager, $setting_id ); |
| 288 | + $this->assertEquals( $initial_post_terms, $setting->value() ); |
| 289 | + $action_count = did_action( 'set_object_terms' ); |
| 290 | + $this->assertTrue( false !== $setting->save() ); |
| 291 | + $this->assertEquals( $action_count + 1, did_action( 'set_object_terms' ) ); |
| 292 | + $this->assertEquals( $modified_post_terms, $setting->value() ); |
| 293 | + $this->assertEquals( $modified_post_terms, wp_list_pluck( get_the_terms( $post_id, $taxonomy ), 'term_id' ) ); |
| 294 | + } |
| 295 | +} |
0 commit comments