|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for Installer Connector class callbacks. |
| 4 | + */ |
| 5 | +namespace WP_Stream; |
| 6 | + |
| 7 | +class Test_WP_Stream_Connector_Installer extends WP_StreamTestCase { |
| 8 | + |
| 9 | + /** |
| 10 | + * Runs before each test |
| 11 | + */ |
| 12 | + public function setUp() { |
| 13 | + parent::setUp(); |
| 14 | + |
| 15 | + // Make partial of Connector_Installer class, with mocked "log" function. |
| 16 | + $this->mock = $this->getMockBuilder( Connector_Installer::class ) |
| 17 | + ->setMethods( array( 'log' ) ) |
| 18 | + ->getMock(); |
| 19 | + |
| 20 | + // Register connector. |
| 21 | + $this->mock->register(); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_callback_upgrader_process_complete() { |
| 25 | + // Prepare scenario |
| 26 | + $this->markTestSkipped( 'This test skipped until the needed scenario can be properly simulated.' ); |
| 27 | + |
| 28 | + // Expected log calls. |
| 29 | + $this->mock->expects( $this->once() ) |
| 30 | + ->method( 'log' ) |
| 31 | + ->withConsecutive( |
| 32 | + array( |
| 33 | + _x( |
| 34 | + 'Installed %1$s: %2$s %3$s', |
| 35 | + 'Plugin/theme installation. 1: Type (plugin/theme), 2: Plugin/theme name, 3: Plugin/theme version', |
| 36 | + 'stream' |
| 37 | + ), |
| 38 | + array( |
| 39 | + 'type' => 'plugin', |
| 40 | + 'name' => 'Hello Dolly', |
| 41 | + 'version' => '', |
| 42 | + 'slug' => 'hello_dolly.php', |
| 43 | + 'success' => true, |
| 44 | + 'error' => null, |
| 45 | + 'old_version' => '', |
| 46 | + ), |
| 47 | + null, |
| 48 | + 'plugins', |
| 49 | + 'installed' |
| 50 | + ), |
| 51 | + array( |
| 52 | + _x( |
| 53 | + 'Updated %1$s: %2$s %3$s', |
| 54 | + 'Plugin/theme update. 1: Type (plugin/theme), 2: Plugin/theme name, 3: Plugin/theme version', |
| 55 | + 'stream' |
| 56 | + ), |
| 57 | + array( |
| 58 | + 'type' => 'theme', |
| 59 | + 'name' => 'Twenty Twenty', |
| 60 | + 'version' => '', |
| 61 | + 'slug' => 'twentytwenty', |
| 62 | + 'success' => true, |
| 63 | + 'error' => null, |
| 64 | + 'old_version' => '', |
| 65 | + ), |
| 66 | + null, |
| 67 | + 'themes', |
| 68 | + 'updated' |
| 69 | + ) |
| 70 | + ); |
| 71 | + |
| 72 | + // Simulate installing plugin and updating theme to trigger callback. |
| 73 | + |
| 74 | + // Check callback test action. |
| 75 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_upgrader_process_complete' ) ); |
| 76 | + } |
| 77 | + |
| 78 | + public function test_callback_activate_plugin() { |
| 79 | + // Expected log calls. |
| 80 | + $this->mock->expects( $this->once() ) |
| 81 | + ->method( 'log' ) |
| 82 | + ->with( |
| 83 | + $this->equalTo( |
| 84 | + _x( |
| 85 | + '"%1$s" plugin activated %2$s', |
| 86 | + '1: Plugin name, 2: Single site or network wide', |
| 87 | + 'stream' |
| 88 | + ) |
| 89 | + ), |
| 90 | + $this->equalTo( |
| 91 | + array( |
| 92 | + 'name' => 'Hello Dolly', |
| 93 | + 'network_wide' => null |
| 94 | + ) |
| 95 | + ), |
| 96 | + $this->equalTo( null ), |
| 97 | + $this->equalTo( 'plugins' ), |
| 98 | + $this->equalTo( 'activated' ) |
| 99 | + ); |
| 100 | + |
| 101 | + // Do stuff. |
| 102 | + \activate_plugin( 'hello.php' ); |
| 103 | + |
| 104 | + // Check callback test action. |
| 105 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_activate_plugin' ) ); |
| 106 | + } |
| 107 | + |
| 108 | + public function test_callback_deactivate_plugin() { |
| 109 | + // Prepare scenario |
| 110 | + \activate_plugin( 'hello.php' ); |
| 111 | + |
| 112 | + // Expected log calls. |
| 113 | + $this->mock->expects( $this->once() ) |
| 114 | + ->method( 'log' ) |
| 115 | + ->with( |
| 116 | + $this->equalTo( |
| 117 | + _x( |
| 118 | + '"%1$s" plugin deactivated %2$s', |
| 119 | + '1: Plugin name, 2: Single site or network wide', |
| 120 | + 'stream' |
| 121 | + ) |
| 122 | + ), |
| 123 | + $this->equalTo( |
| 124 | + array( |
| 125 | + 'name' => 'Hello Dolly', |
| 126 | + 'network_wide' => null, |
| 127 | + ) |
| 128 | + ), |
| 129 | + $this->equalTo( null ), |
| 130 | + $this->equalTo( 'plugins' ), |
| 131 | + $this->equalTo( 'deactivated' ) |
| 132 | + ); |
| 133 | + |
| 134 | + // Do stuff. |
| 135 | + \deactivate_plugins( array( 'hello.php' ) ); |
| 136 | + |
| 137 | + // Check callback test action. |
| 138 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_deactivate_plugin' ) ); |
| 139 | + } |
| 140 | + |
| 141 | + public function test_callback_switch_theme() { |
| 142 | + // Expected log calls. |
| 143 | + $this->mock->expects( $this->once() ) |
| 144 | + ->method( 'log' ) |
| 145 | + ->with( |
| 146 | + $this->equalTo( __( '"%s" theme activated', 'stream' ) ), |
| 147 | + $this->equalTo( array( 'name' => 'Twenty Twenty' ) ), |
| 148 | + $this->equalTo( null ), |
| 149 | + $this->equalTo( 'themes' ), |
| 150 | + $this->equalTo( 'activated' ) |
| 151 | + ); |
| 152 | + |
| 153 | + // Do stuff. |
| 154 | + switch_theme( 'twentytwenty' ); |
| 155 | + |
| 156 | + // Check callback test action. |
| 157 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_switch_theme' ) ); |
| 158 | + } |
| 159 | + |
| 160 | + public function test_callback_delete_site_transient_update_themes() { |
| 161 | + // Expected log calls. |
| 162 | + $this->mock->expects( $this->once() ) |
| 163 | + ->method( 'log' ) |
| 164 | + ->with( |
| 165 | + $this->equalTo( __( '"%s" theme deleted', 'stream' ) ), |
| 166 | + $this->equalTo( array( 'name' => 'twentyninteen' ) ), |
| 167 | + $this->equalTo( null ), |
| 168 | + $this->equalTo( 'themes' ), |
| 169 | + $this->equalTo( 'deleted' ) |
| 170 | + ); |
| 171 | + |
| 172 | + // Do stuff. |
| 173 | + delete_theme( 'twentyninteen' ); |
| 174 | + |
| 175 | + // Check callback test action. |
| 176 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_delete_site_transient_update_themes' ) ); |
| 177 | + } |
| 178 | + |
| 179 | + public function test_callback_pre_set_site_transient_update_plugins() { |
| 180 | + // Prepare scenario |
| 181 | + $this->markTestSkipped( 'This test skipped until the needed scenario can be properly simulated.' ); |
| 182 | + |
| 183 | + // Expected log calls. |
| 184 | + $this->mock->expects( $this->once() ) |
| 185 | + ->method( 'log' ) |
| 186 | + ->with( |
| 187 | + $this->equalTo( __( '"%s" plugin deleted', 'stream' ) ), |
| 188 | + $this->equalTo( |
| 189 | + array( |
| 190 | + 'name' => 'Hello Dolly', |
| 191 | + 'plugin' => 'hello.php', |
| 192 | + 'network_wide' => null, |
| 193 | + ) |
| 194 | + ), |
| 195 | + $this->equalTo( null ), |
| 196 | + $this->equalTo( 'plugins' ), |
| 197 | + $this->equalTo( 'deleted' ) |
| 198 | + ); |
| 199 | + |
| 200 | + // Do stuff. |
| 201 | + |
| 202 | + |
| 203 | + // Check callback test action. |
| 204 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_pre_set_site_transient_update_plugins' ) ); |
| 205 | + } |
| 206 | + |
| 207 | + public function test_callback__core_updated_successfully() { |
| 208 | + // Prepare scenario |
| 209 | + $this->markTestSkipped( 'This test skipped until the needed scenario can be properly simulated.' ); |
| 210 | + |
| 211 | + // Expected log calls. |
| 212 | + $this->mock->expects( $this->exactly( 2 ) ) |
| 213 | + ->method( 'log' ) |
| 214 | + ->withConsecutive( |
| 215 | + array( |
| 216 | + $this->equalTo( esc_html__( 'WordPress auto-updated to %s', 'stream' ) ), |
| 217 | + $this->equalTo( |
| 218 | + array( |
| 219 | + 'new_version' => '', |
| 220 | + 'old_version' => '', |
| 221 | + 'auto_updated' => true, |
| 222 | + ) |
| 223 | + ), |
| 224 | + $this->equalTo( null ), |
| 225 | + $this->equalTo( 'WordPress' ), |
| 226 | + $this->equalTo( 'updated' ) |
| 227 | + ), |
| 228 | + array( |
| 229 | + $this->equalTo( esc_html__( 'WordPress updated to %s', 'stream' ) ), |
| 230 | + $this->equalTo( |
| 231 | + array( |
| 232 | + 'new_version' => '', |
| 233 | + 'old_version' => '', |
| 234 | + 'auto_updated' => false, |
| 235 | + ) |
| 236 | + ), |
| 237 | + $this->equalTo( null ), |
| 238 | + $this->equalTo( 'WordPress' ), |
| 239 | + $this->equalTo( 'updated' ) |
| 240 | + ) |
| 241 | + ); |
| 242 | + |
| 243 | + // Do stuff. |
| 244 | + |
| 245 | + // Check callback test action. |
| 246 | + $this->assertFalse( 0 === did_action( $this->action_prefix . 'callback__core_updated_successfully' ) ); |
| 247 | + } |
| 248 | +} |
0 commit comments