| | 3068 | |
| | 3069 | /** |
| | 3070 | * Test that a script is moved to the footer if it is made non deferrable, was in the header and |
| | 3071 | * all scripts that depend on it are in the footer. |
| | 3072 | * |
| | 3073 | * @ticket 58599 |
| | 3074 | * |
| | 3075 | * @dataProvider data_provider_script_move_to_footer |
| | 3076 | * |
| | 3077 | * @param string $b_in_footer in_footer value for script B. |
| | 3078 | * @param string $expected Expected output. |
| | 3079 | * @param bool $expect_a_in_footer Is script A in the footer? |
| | 3080 | * @param bool $expect_b_in_footer Is script B in the footer? |
| | 3081 | */ |
| | 3082 | public function test_wp_scripts_move_to_footer( $b_in_footer, $expected, $expect_a_in_footer, $expect_b_in_footer ) { |
| | 3083 | // Script A uses strategy=>defer. Script B depends on script A and uses in_footer=>true. |
| | 3084 | wp_enqueue_script( 'script-a', 'script-a.js', array(), null, array( 'strategy' => 'defer' ) ); |
| | 3085 | wp_enqueue_script( 'script-b', 'script-b.js', array( 'script-a' ), null, $b_in_footer ); |
| | 3086 | |
| | 3087 | // Run the header and footer capturing the output. |
| | 3088 | ob_start(); |
| | 3089 | wp_scripts()->do_head_items(); |
| | 3090 | wp_scripts()->do_footer_items(); |
| | 3091 | $actual = ob_get_clean(); |
| | 3092 | |
| | 3093 | $this->assertEquals( $expected, $actual ); |
| | 3094 | $this->assertEquals( $expect_a_in_footer, in_array( 'script-a', wp_scripts()->in_footer, true ) ); |
| | 3095 | $this->assertEquals( $expect_b_in_footer, in_array( 'script-b', wp_scripts()->in_footer, true ) ); |
| | 3096 | } |
| | 3097 | |
| | 3098 | /** |
| | 3099 | * Data provider for test_wp_scripts_move_to_footer. |
| | 3100 | * |
| | 3101 | * @return array[] { |
| | 3102 | * Array of arguments for test. |
| | 3103 | * |
| | 3104 | * @type string $b_in_footer in_footer value for script B. |
| | 3105 | * @type string $expected Expected output. |
| | 3106 | * } |
| | 3107 | */ |
| | 3108 | public function data_provider_script_move_to_footer() { |
| | 3109 | return array( |
| | 3110 | |
| | 3111 | 'b in the footer, not deferred' => array( |
| | 3112 | // Script B is in the footer, but not deferred. |
| | 3113 | 'b_in_footer' => true, |
| | 3114 | 'expect' => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" data-wp-strategy="defer"></script> |
| | 3115 | <script type="text/javascript" src="http://script-b.js" id="script-b-js"></script> |
| | 3116 | ', |
| | 3117 | 'expect_a_in_footer' => true, |
| | 3118 | 'expect_b_in_footer' => true, |
| | 3119 | ), |
| | 3120 | 'b in header not deferred' => array( |
| | 3121 | 'b_in_footer' => false, |
| | 3122 | 'expect' => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" data-wp-strategy="defer"></script> |
| | 3123 | <script type="text/javascript" src="http://script-b.js" id="script-b-js"></script> |
| | 3124 | ', |
| | 3125 | 'expect_a_in_footer' => false, |
| | 3126 | 'expect_b_in_footer' => false, |
| | 3127 | ), |
| | 3128 | 'b in footer and deferred' => array( |
| | 3129 | 'b_in_footer' => array( |
| | 3130 | 'strategy' => 'defer', |
| | 3131 | 'in_footer' => true, |
| | 3132 | ), |
| | 3133 | 'expect' => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" defer="defer" data-wp-strategy="defer"></script> |
| | 3134 | <script type="text/javascript" src="http://script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script> |
| | 3135 | ', |
| | 3136 | 'expect_a_in_footer' => false, |
| | 3137 | 'expect_b_in_footer' => true, |
| | 3138 | ), |
| | 3139 | 'b in header and deferred' => array( |
| | 3140 | 'b_in_footer' => array( 'strategy' => 'defer' ), |
| | 3141 | 'expect' => '<script type="text/javascript" src="http://script-a.js" id="script-a-js" defer="defer" data-wp-strategy="defer"></script> |
| | 3142 | <script type="text/javascript" src="http://script-b.js" id="script-b-js" defer="defer" data-wp-strategy="defer"></script> |
| | 3143 | ', |
| | 3144 | 'expect_a_in_footer' => false, |
| | 3145 | 'expect_b_in_footer' => false, |
| | 3146 | ), |
| | 3147 | ); |
| | 3148 | } |