|
1 | 1 | <?php
|
2 | 2 | namespace WP_Stream;
|
3 | 3 | /**
|
4 |
| - * Class Test_Connector_Settings |
| 4 | + * Tests for Settings connector class callbacks. |
5 | 5 | *
|
6 | 6 | * @package WP_Stream
|
7 | 7 | */
|
8 | 8 | class Test_Connector_Settings extends WP_StreamTestCase {
|
9 |
| - |
10 | 9 | /**
|
11 |
| - * Holds the connector settings base class. |
12 |
| - * |
13 |
| - * @var Connector_Settings |
| 10 | + * Runs before each test. |
14 | 11 | */
|
15 |
| - protected $connector; |
16 |
| - |
17 | 12 | public function setUp() {
|
18 | 13 | parent::setUp();
|
19 |
| - $this->connector = new Connector_Settings(); |
20 | 14 |
|
21 |
| - $this->assertNotEmpty( $this->connector ); |
| 15 | + $this->plugin->connectors->unload_connectors(); |
| 16 | + |
| 17 | + // Make partial of Connector_Settings class, with mocked "log" function. |
| 18 | + $this->mock = $this->getMockBuilder( Connector_Settings::class ) |
| 19 | + ->setMethods( array( 'log' ) ) |
| 20 | + ->getMock(); |
| 21 | + |
| 22 | + // Register connector. |
| 23 | + $this->mock->register(); |
22 | 24 | }
|
23 | 25 |
|
24 | 26 | public function test_is_option_ignored() {
|
25 |
| - $this->assertTrue( $this->connector->is_option_ignored( '_transient_option_name' ) ); |
26 |
| - $this->assertTrue( $this->connector->is_option_ignored( '_site_transient_option_name' ) ); |
27 |
| - $this->assertTrue( $this->connector->is_option_ignored( 'option_name$' ) ); |
28 |
| - $this->assertTrue( $this->connector->is_option_ignored( 'image_default_link_type' ) ); |
29 |
| - $this->assertTrue( $this->connector->is_option_ignored( 'medium_large_size_w' ) ); |
30 |
| - $this->assertTrue( $this->connector->is_option_ignored( 'medium_large_size_h' ) ); |
31 |
| - |
32 |
| - $this->assertFalse( $this->connector->is_option_ignored( 'option_site_transient_name' ) ); |
33 |
| - $this->assertFalse( $this->connector->is_option_ignored( 'option_transient_name' ) ); |
34 |
| - $this->assertFalse( $this->connector->is_option_ignored( 'option_$_name' ) ); |
35 |
| - $this->assertFalse( $this->connector->is_option_ignored( 'not_ignored' ) ); |
| 27 | + $this->assertTrue( $this->mock->is_option_ignored( '_transient_option_name' ) ); |
| 28 | + $this->assertTrue( $this->mock->is_option_ignored( '_site_transient_option_name' ) ); |
| 29 | + $this->assertTrue( $this->mock->is_option_ignored( 'option_name$' ) ); |
| 30 | + $this->assertTrue( $this->mock->is_option_ignored( 'image_default_link_type' ) ); |
| 31 | + $this->assertTrue( $this->mock->is_option_ignored( 'medium_large_size_w' ) ); |
| 32 | + $this->assertTrue( $this->mock->is_option_ignored( 'medium_large_size_h' ) ); |
| 33 | + |
| 34 | + $this->assertFalse( $this->mock->is_option_ignored( 'option_site_transient_name' ) ); |
| 35 | + $this->assertFalse( $this->mock->is_option_ignored( 'option_transient_name' ) ); |
| 36 | + $this->assertFalse( $this->mock->is_option_ignored( 'option_$_name' ) ); |
| 37 | + $this->assertFalse( $this->mock->is_option_ignored( 'not_ignored' ) ); |
36 | 38 |
|
37 | 39 | // Test custom ignores.
|
38 |
| - $this->assertFalse( $this->connector->is_option_ignored( 'ignore_me' ) ); |
| 40 | + $this->assertFalse( $this->mock->is_option_ignored( 'ignore_me' ) ); |
39 | 41 |
|
40 | 42 | add_filter( 'wp_stream_is_option_ignored', function( $is_ignored, $option_name, $default_ignored ) {
|
41 | 43 | return in_array( $option_name, array_merge( [ 'ignore_me' ], $default_ignored ), true );
|
42 | 44 | }, 10, 3 );
|
43 | 45 |
|
44 |
| - $this->assertTrue( $this->connector->is_option_ignored( 'ignore_me' ) ); |
| 46 | + $this->assertTrue( $this->mock->is_option_ignored( 'ignore_me' ) ); |
45 | 47 | }
|
46 | 48 |
|
| 49 | + public function test_callback_updated_option() { |
| 50 | + // If multisite use site_option methods and test "update_site_option" callback |
| 51 | + // instead of the update_option callback. |
| 52 | + $add_method = is_multisite() ? 'add_site_option' : 'add_option'; |
| 53 | + $update_method = is_multisite() ? 'update_site_option' : 'update_option'; |
| 54 | + |
| 55 | + // Create options in database for later use. |
| 56 | + call_user_func( $add_method, 'users_can_register', 0 ); |
| 57 | + call_user_func( $add_method, 'permalink_structure', '' ); |
| 58 | + call_user_func( $add_method, 'category_base', '' ); |
| 59 | + call_user_func( $add_method, 'tag_base', '' ); |
| 60 | + |
| 61 | + $this->mock->expects( $this->exactly( 4 ) ) |
| 62 | + ->method( 'log' ) |
| 63 | + ->withConsecutive( |
| 64 | + array( |
| 65 | + $this->equalTo( __( '"%s" setting was updated', 'stream' ) ), |
| 66 | + $this->equalTo( |
| 67 | + array( |
| 68 | + 'label' => 'Membership', |
| 69 | + 'option' => 'users_can_register', |
| 70 | + 'context' => 'settings', |
| 71 | + 'old_value' => '0', |
| 72 | + 'value' => '1', |
| 73 | + ) |
| 74 | + ), |
| 75 | + $this->equalTo( null ), |
| 76 | + $this->equalTo( 'settings' ), |
| 77 | + $this->equalTo( 'updated' ) |
| 78 | + ), |
| 79 | + array( |
| 80 | + $this->equalTo( __( '"%s" setting was updated', 'stream' ) ), |
| 81 | + $this->equalTo( |
| 82 | + array( |
| 83 | + 'label' => 'Permalink Settings', |
| 84 | + 'option' => 'permalink_structure', |
| 85 | + 'context' => 'permalink', |
| 86 | + 'old_value' => '', |
| 87 | + 'value' => '/%year%/%postname%/', |
| 88 | + ) |
| 89 | + ), |
| 90 | + $this->equalTo( null ), |
| 91 | + $this->equalTo( 'permalink' ), |
| 92 | + $this->equalTo( 'updated' ) |
| 93 | + ), |
| 94 | + array( |
| 95 | + $this->equalTo( __( '"%s" setting was updated', 'stream' ) ), |
| 96 | + $this->equalTo( |
| 97 | + array( |
| 98 | + 'label' => 'Category base', |
| 99 | + 'option' => 'category_base', |
| 100 | + 'context' => 'permalink', |
| 101 | + 'old_value' => '', |
| 102 | + 'value' => 'cat/', |
| 103 | + ) |
| 104 | + ), |
| 105 | + $this->equalTo( null ), |
| 106 | + $this->equalTo( 'permalink' ), |
| 107 | + $this->equalTo( 'updated' ) |
| 108 | + ), |
| 109 | + array( |
| 110 | + $this->equalTo( __( '"%s" setting was updated', 'stream' ) ), |
| 111 | + $this->equalTo( |
| 112 | + array( |
| 113 | + 'label' => 'Tag base', |
| 114 | + 'option' => 'tag_base', |
| 115 | + 'context' => 'permalink', |
| 116 | + 'old_value' => '', |
| 117 | + 'value' => 'tag/', |
| 118 | + ) |
| 119 | + ), |
| 120 | + $this->equalTo( null ), |
| 121 | + $this->equalTo( 'permalink' ), |
| 122 | + $this->equalTo( 'updated' ) |
| 123 | + ) |
| 124 | + ); |
| 125 | + |
| 126 | + // Simulate being on the WP Customizr page. |
| 127 | + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
| 128 | + do_action( 'customize_save', new \WP_Customize_Manager( array() ) ); |
| 129 | + |
| 130 | + // Update options to trigger callback. |
| 131 | + call_user_func( $update_method, 'users_can_register', 1 ); |
| 132 | + |
| 133 | + // Use this to prevent repeated log calls. |
| 134 | + global $wp_actions; |
| 135 | + unset( $wp_actions['customize_save'] ); |
| 136 | + |
| 137 | + call_user_func( $update_method, 'permalink_structure', '/%year%/%postname%/' ); |
| 138 | + call_user_func( $update_method, 'category_base', 'cat/' ); |
| 139 | + call_user_func( $update_method, 'tag_base', 'tag/' ); |
| 140 | + |
| 141 | + // If multisite only check update_site_option test callback. |
| 142 | + if ( is_multisite() ) { |
| 143 | + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_site_option' ) ); |
| 144 | + } else { |
| 145 | + // Check callback test action. |
| 146 | + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_tag_base' ) ); |
| 147 | + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_category_base' ) ); |
| 148 | + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option_permalink_structure' ) ); |
| 149 | + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_updated_option' ) ); |
| 150 | + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_update_option' ) ); |
| 151 | + } |
| 152 | + } |
47 | 153 | }
|
0 commit comments