|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once JETPACK__PLUGIN_DIR . '3rd-party/domain-mapping.php'; |
| 4 | + |
| 5 | +// Extend with a public constructor so we can test |
| 6 | +class MockDomainMapping extends Jetpack_3rd_Party_Domain_Mapping { |
| 7 | + public function __construct() { |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +class WP_Test_Domain_Mapping extends WP_UnitTestCase { |
| 12 | + function tearDown() { |
| 13 | + Jetpack_Constants::clear_constants(); |
| 14 | + foreach ( $this->get_jetpack_sync_filters() as $filter ) { |
| 15 | + remove_all_filters( $filter ); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + function test_domain_mapping_should_not_try_to_hook_when_sunrise_disable() { |
| 20 | + $stub = $this->getMockBuilder( 'MockDomainMapping' ) |
| 21 | + ->setMethods( array( 'hook_wordpress_mu_domain_mapping', 'hook_wpmu_dev_domain_mapping' ) ) |
| 22 | + ->disableOriginalConstructor() |
| 23 | + ->getMock(); |
| 24 | + |
| 25 | + // Both of these methods should not be called |
| 26 | + $stub->expects( $this->exactly( 0 ) ) |
| 27 | + ->method( 'hook_wordpress_mu_domain_mapping' ) |
| 28 | + ->will( $this->returnValue( false ) ); |
| 29 | + |
| 30 | + $stub->expects( $this->exactly( 0 ) ) |
| 31 | + ->method( 'hook_wpmu_dev_domain_mapping' ) |
| 32 | + ->will( $this->returnValue( false ) ); |
| 33 | + |
| 34 | + $stub->attempt_to_hook_domain_mapping_plugins(); |
| 35 | + } |
| 36 | + |
| 37 | + function test_domain_mapping_should_stop_search_after_hooking_once() { |
| 38 | + Jetpack_Constants::set_constant( 'SUNRISE', true ); |
| 39 | + |
| 40 | + $stub = $this->getMockBuilder( 'MockDomainMapping' ) |
| 41 | + ->setMethods( array( 'hook_wordpress_mu_domain_mapping', 'hook_wpmu_dev_domain_mapping' ) ) |
| 42 | + ->disableOriginalConstructor() |
| 43 | + ->getMock(); |
| 44 | + |
| 45 | + // The first method in the array should be the only one called. |
| 46 | + $stub->expects( $this->exactly( 1 ) ) |
| 47 | + ->method( 'hook_wordpress_mu_domain_mapping' ) |
| 48 | + ->will( $this->returnValue( true ) ); |
| 49 | + |
| 50 | + $stub->expects( $this->exactly( 0 ) ) |
| 51 | + ->method( 'hook_wpmu_dev_domain_mapping' ) |
| 52 | + ->will( $this->returnValue( false ) ); |
| 53 | + |
| 54 | + $stub->attempt_to_hook_domain_mapping_plugins(); |
| 55 | + } |
| 56 | + |
| 57 | + function test_domain_mapping_mu_domain_mapping_not_hooked_when_function_not_exists() { |
| 58 | + Jetpack_Constants::set_constant( 'DOMAIN_MAPPING', true ); |
| 59 | + |
| 60 | + $stub = $this->getMockBuilder( 'MockDomainMapping' ) |
| 61 | + ->setMethods( array( 'function_exists' ) ) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $stub->expects( $this->once() ) |
| 66 | + ->method( 'function_exists' ) |
| 67 | + ->will( $this->returnValue( false ) ); |
| 68 | + |
| 69 | + $this->assertFalse( $stub->hook_wordpress_mu_domain_mapping() ); |
| 70 | + |
| 71 | + foreach ( $this->get_jetpack_sync_filters() as $filter ) { |
| 72 | + $this->assertFalse( $this->filter_has_hook( $filter ) ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function test_domain_mapping_mu_domain_mapping_hooked_when_function_exists() { |
| 77 | + Jetpack_Constants::set_constant( 'DOMAIN_MAPPING', true ); |
| 78 | + |
| 79 | + $stub = $this->getMockBuilder( 'MockDomainMapping' ) |
| 80 | + ->setMethods( array( 'function_exists' ) ) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->getMock(); |
| 83 | + |
| 84 | + $stub->expects( $this->once() ) |
| 85 | + ->method( 'function_exists' ) |
| 86 | + ->will( $this->returnValue( true ) ); |
| 87 | + |
| 88 | + $this->assertTrue( $stub->hook_wordpress_mu_domain_mapping() ); |
| 89 | + |
| 90 | + foreach ( $this->get_jetpack_sync_filters() as $filter ) { |
| 91 | + $this->assertTrue( $this->filter_has_hook( $filter ) ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + function test_domain_mapping_wpmu_dev_domain_mapping_not_hooked_when_functions_not_exist() { |
| 96 | + $stub = $this->getMockBuilder( 'MockDomainMapping' ) |
| 97 | + ->setMethods( array( 'class_exists', 'method_exists' ) ) |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->getMock(); |
| 100 | + |
| 101 | + $stub->expects( $this->once() ) |
| 102 | + ->method( 'class_exists' ) |
| 103 | + ->will( $this->returnValue( false ) ); |
| 104 | + |
| 105 | + $stub->expects( $this->exactly( 0 ) ) |
| 106 | + ->method( 'method_exists' ) |
| 107 | + ->will( $this->returnValue( false ) ); |
| 108 | + |
| 109 | + $this->assertFalse( $stub->hook_wpmu_dev_domain_mapping() ); |
| 110 | + |
| 111 | + foreach ( $this->get_jetpack_sync_filters() as $filter ) { |
| 112 | + $this->assertFalse( $this->filter_has_hook( $filter ) ); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + function test_domain_mapping_wpmu_dev_domain_mapping_hooked_when_functions_exist() { |
| 117 | + $stub = $this->getMockBuilder( 'MockDomainMapping' ) |
| 118 | + ->setMethods( array( 'class_exists', 'method_exists', 'get_domain_mapping_utils_instance' ) ) |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + |
| 122 | + $stub->expects( $this->once() ) |
| 123 | + ->method( 'class_exists' ) |
| 124 | + ->will( $this->returnValue( true ) ); |
| 125 | + |
| 126 | + $stub->expects( $this->once() ) |
| 127 | + ->method( 'method_exists' ) |
| 128 | + ->will( $this->returnValue( true ) ); |
| 129 | + |
| 130 | + $stub->expects( $this->once() ) |
| 131 | + ->method( 'get_domain_mapping_utils_instance' ) |
| 132 | + ->will( $this->returnValue( new stdClass() ) ); |
| 133 | + |
| 134 | + $this->assertTrue( $stub->hook_wpmu_dev_domain_mapping() ); |
| 135 | + |
| 136 | + foreach ( $this->get_jetpack_sync_filters() as $filter ) { |
| 137 | + $this->assertTrue( $this->filter_has_hook( $filter ) ); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + function filter_has_hook( $hook ) { |
| 142 | + global $wp_filter; |
| 143 | + return isset( $wp_filter[ $hook ] ) && ! empty( $wp_filter[ $hook ] ); |
| 144 | + } |
| 145 | + |
| 146 | + function get_jetpack_sync_filters() { |
| 147 | + return array( |
| 148 | + 'jetpack_sync_home_url', |
| 149 | + 'jetpack_sync_site_url', |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments