| 1 |  |  | <?php // phpcs:ignore WordPress.Files.FileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Acceptance test suite for the current autoloader. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package automattic/jetpack-autoloader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Test suite class for verifying the functionality of the current autoloader's cache mechanism. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @runTestsInSeparateProcesses Ensure each test has a fresh process as if it was a real request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @preserveGlobalState disabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class CacheTest extends Acceptance_Test_Case { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * Tests that the autoloader erases the cache if the shutdown action happens before plugins are finished loading. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	public function test_autoloader_does_not_save_cache_on_early_shutdown() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		// Cache the plugin so we can detect if it was erased. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		$this->cache_plugin( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		// Load and prematurely shutdown the autoloader. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$this->load_plugin_autoloader( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$this->trigger_shutdown( false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		// Make sure the autoloader worked as expected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$this->assertAutoloaderVersion( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		$this->assertAutoloaderResetCount( 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		$this->assertAutoloaderFoundUnknown( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		$this->assertAutoloaderCacheEmpty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Current\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * Tests that the autoloader does not write the cache when the active plugins have not changed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 38 |  | View Code Duplication | 	public function test_autoloader_does_not_write_unchanged_cache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		// Cache the plugin so that the cache is unchanged. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$this->cache_plugin( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		// Load the autoloader. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$this->load_plugin_autoloader( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		// We're going to erase the cache before we shut the autoloader down. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		// Since the cache is only loaded when the autoloader is, we can | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		// do this to detect whether or not the cache is updated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->erase_cache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		// Trigger the shutdown now and it should NOT update the cache. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->trigger_shutdown( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		// Make sure the autoloader worked as expected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$this->assertAutoloaderVersion( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$this->assertAutoloaderResetCount( 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->assertAutoloaderFoundUnknown( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->assertAutoloaderCacheEmpty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Current\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * Tests that the autoloader does not reset when it encounters unknown plugins that are in the cache already. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication | 	public function test_autoloader_does_not_reset_when_unknown_plugins_are_cached() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		// Cache the plugins so that they will not be unknown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->cache_plugin( array( self::CURRENT_MU, Test_Plugin_Factory::CURRENT ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		// Load and shutdown the autoloaders safely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$this->execute_autoloader_chain( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 				self::CURRENT_MU, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				Test_Plugin_Factory::CURRENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		// Make sure the autoloader worked as expected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$this->assertAutoloaderVersion( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$this->assertAutoloaderResetCount( 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$this->assertAutoloaderFoundUnknown( self::CURRENT_MU ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$this->assertAutoloaderCacheEquals( array( self::CURRENT_MU, Test_Plugin_Factory::CURRENT ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\SharedTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$this->markTestIncomplete( 'The autoloader does not currently support PSR-4 loading from multiple directories.' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Current\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Currentmu\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * Tests that the autoloader is able to add new plugins to the cache. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 92 |  | View Code Duplication | 	public function test_autoloader_adds_new_plugins_to_cache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		// Cache only one of the plugins so that it won't be unknown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$this->cache_plugin( array( Test_Plugin_Factory::CURRENT ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		// Load and shutdown the autoloaders safely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$this->execute_autoloader_chain( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 				self::CURRENT_MU, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				Test_Plugin_Factory::CURRENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		// Make sure the autoloader worked as expected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$this->assertAutoloaderVersion( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$this->assertAutoloaderResetCount( 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$this->assertAutoloaderFoundUnknown( self::CURRENT_MU ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$this->assertAutoloaderCacheEquals( array( self::CURRENT_MU, Test_Plugin_Factory::CURRENT ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\SharedTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$this->markTestIncomplete( 'The autoloader does not currently support PSR-4 loading from multiple directories.' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Current\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Currentmu\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * Tests that the autoloader resolves symlinks to plugins pulled from cache so that those environments can be handled correctly. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  | 	 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 120 |  | View Code Duplication | 	public function test_autoloader_resolves_cached_symlinks() { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 		$symlink_key = 'current_symlink'; | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 		// Install the autoloader as a symlink so that we can execute it. | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 		$this->install_autoloader_symlink( Test_Plugin_Factory::CURRENT, false, $symlink_key ); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 		// Store the symlink in the cache so that we can make sure it is resolved correctly. | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 		$this->cache_plugin( $symlink_key ); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 		// Load and shutdown the autoloader safely. | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 		$this->load_plugin_autoloader( $symlink_key ); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 		$this->trigger_shutdown( true ); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  | 		// Make sure the autoloader worked as expected. | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 		$this->assertAutoloaderVersion( Test_Plugin_Factory::CURRENT ); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  | 		$this->assertAutoloaderResetCount( 0 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  | 		$this->assertAutoloaderNotFoundUnknown( $symlink_key ); | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | 		// The cache shouldn't get updated since nothing has technically changed. | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  | 		$this->assertAutoloaderCacheEquals( $symlink_key ); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Current\UniqueTestClass::class ); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * Tests that the autoloader does not cache plugins that are about to deactivate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	public function test_autoloader_does_not_cache_deactivating_plugins() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		// Store the plugin in the cache so that we can watch for removal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$this->cache_plugin( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		// Make sure that we're deactivating the plugin in the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		$_REQUEST['_wpnonce'] = '123abc'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		$_REQUEST['action']   = 'deactivate'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		$_REQUEST['plugin']   = Test_Plugin_Factory::CURRENT . '/' . Test_Plugin_Factory::CURRENT . '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		// Load and shutdown the autoloader safely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		$this->load_plugin_autoloader( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		$this->trigger_shutdown( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		// Make sure the autoloader worked as expected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$this->assertAutoloaderVersion( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		$this->assertAutoloaderResetCount( 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		// Even though not active the plugin should be seen becasue of the request parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		$this->assertAutoloaderNotFoundUnknown( Test_Plugin_Factory::CURRENT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		// The cache should be empty since the deactivating plugin was removed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$this->assertAutoloaderCacheEmpty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$this->assertAutoloaderProvidesClass( \Automattic\Jetpack\AutoloaderTesting\Current\UniqueTestClass::class ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |