| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Automattic\Jetpack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Automattic\Jetpack\JITM; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use phpmock\functions\FunctionProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use phpmock\Mock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use phpmock\MockBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  | class Test_Jetpack_JITM extends TestCase { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | 	public function setUp() { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | 		$this->mock_add_action(); | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | 		$this->mock_do_action(); | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 		$this->mock_wp_enqueue_script(); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 		// input/output of these functions doesn't matter right now, they just need to exist | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 		$this->mock_empty_function( 'wp_register_style' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 		$this->mock_empty_function( 'plugins_url' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 		$this->mock_empty_function( 'wp_style_add_data' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 		$this->mock_empty_function( 'wp_enqueue_style' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 		$this->mock_empty_function( 'wp_localize_script' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 		$this->mock_empty_function( 'esc_url_raw' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		$this->mock_empty_function( 'rest_url' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		$this->mock_empty_function( 'esc_html__' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	public function tearDown() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		Mock::disableAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		\Mockery::close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		$this->clear_added_actions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$this->clear_enqueued_scripts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 35 |  | View Code Duplication | 	public function test_jitm_disabled_by_filter() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$this->mock_filters( array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			array( 'jetpack_just_in_time_msgs', false, false ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$jitm = new JITM(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$this->assertFalse( $jitm->register() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$this->clear_mock_filters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 46 |  | View Code Duplication | 	public function test_jitm_enabled_by_default() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$this->mock_filters( array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			array( 'jetpack_just_in_time_msgs', false, true ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$jitm = new JITM(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->assertTrue( $jitm->register() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$this->clear_mock_filters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * This is an example of a test which uses Mockery to tests a class static method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * It requires the runInSeparateProcess tag so that the class isn't already autoloaded. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * @runInSeparateProcess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	public function test_prepare_jitms_enqueues_assets() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$mockAssets = \Mockery::mock('alias:Automattic\Jetpack\Assets'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		// mock the static method and return a dummy value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$mockAssets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			->shouldReceive('get_file_url_for_environment') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			->andReturn('the_file_url'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$jitm = new JITM(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		$screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$jitm->prepare_jitms( $screen ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		// this should enqueue a jetpack-jitm-new script | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		do_action( 'admin_enqueue_scripts' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		// assert our script was enqueued with the right value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$script = $this->get_enqueued_script( 'jetpack-jitm-new' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$this->assertEquals( 'the_file_url', $script['src'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	public function test_prepare_jitms_does_not_show_on_some_screens() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$jitm = new JITM(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$screen = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$screen->id = 'jetpack_page_stats'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$jitm->prepare_jitms( $screen ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	protected function mock_filters( $filters ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$this->mocked_filters = $filters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$builder = new MockBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$builder->setNamespace( __NAMESPACE__ ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			->setName( 'apply_filters' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			->setFunction( | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 100 |  | View Code Duplication | 				function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 					$current_args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 					foreach ( $this->mocked_filters as $filter ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 						if ( array_slice( $filter, 0, -1 ) === $current_args ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 							return array_pop( $filter ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 						} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$this->apply_filters_mock = $builder->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$this->apply_filters_mock->enable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	protected function clear_mock_filters() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$this->apply_filters_mock->disable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		unset( $this->mocked_filters ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	protected function mock_add_action() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$builder = new MockBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		$builder->setNamespace( __NAMESPACE__ ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			->setName( 'add_action' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			->setFunction( function( $name, $callable ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 				global $actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				if ( is_null( $actions ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 					$actions = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				// don't worry about precedence for now | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 				if ( ! isset( $actions[$name] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 					$actions[$name] = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 				$actions[$name][] = $callable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			} ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$builder->build()->enable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	protected function mock_do_action() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$builder = new MockBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$builder->setNamespace( __NAMESPACE__ ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			->setName( 'do_action' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			->setFunction( function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				global $actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 				$args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 				$name = array_shift( $args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 				if ( is_null( $actions ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 					$actions = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 				// don't worry about precedence for now | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 				if ( ! isset( $actions[$name] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 					$actions[$name] = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 				foreach( $actions[$name] as $callable ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 					call_user_func_array( $callable, $args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			} ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$builder->build()->enable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	protected function mock_wp_enqueue_script() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$builder = new MockBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		$builder->setNamespace( __NAMESPACE__ ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 			->setName( 'wp_enqueue_script' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			->setFunction( function( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 				global $wp_scripts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 				if ( is_null( $wp_scripts ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 					$wp_scripts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 				$wp_scripts[$handle] = compact( 'src', 'deps', 'ver', 'in_footer' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			} ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		$builder->build()->enable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	protected function get_enqueued_script( $handle ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		global $wp_scripts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		return isset( $wp_scripts[$handle] ) ? $wp_scripts[$handle] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	protected function clear_added_actions() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		global $actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 		$actions = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	protected function clear_enqueued_scripts() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		global $wp_scripts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		$wp_scripts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	protected function mock_empty_function( $name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 		$builder = new MockBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 		$builder->setNamespace( __NAMESPACE__ ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 			->setName( $name ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 			->setFunction( function() use ( $name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 				// echo "Called $name with " . print_r( func_get_args(),1 ) . "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 			} ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		$builder->build()->enable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |  |