[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/plugins/ -> files.php (source)

   1  <?php
   2  
   3  /**
   4   * File related plugin functions : find plugins, activate, deactivate, uninstall.
   5   *
   6   * @group plugins
   7   */
   8  class Plugin_Files_Tests extends PHPUnit\Framework\TestCase {
   9  
  10      /**
  11       * Reset active plugin list
  12       */
  13      public static function tearDownAfterClass(): void {
  14          yourls_get_db()->set_plugins( array() );
  15      }
  16  
  17      /**
  18       * Return array of files from tests/data/plugins/invalid-code
  19       */
  20      public function get_invalid_code_plugins() {
  21          $plugins = array();
  22          foreach ( glob(YOURLS_PLUGINDIR . '/invalid-code/*.php') as $file ) {
  23              $plugins[] = array($file);
  24          }
  25          return $plugins;
  26      }
  27  
  28      /**
  29       * Return one of the "valid" test plugins from tests/data/plugins
  30       */
  31  	public function pick_a_plugin() {
  32          $plugins = array_keys( yourls_get_plugins() );
  33          $plugin = $plugins[ array_rand( $plugins ) ];
  34          return $plugin;
  35      }
  36  
  37      /**
  38       * Check that only 2 "valid" plugins are found in tests/data/plugins
  39       */
  40  	public function test_get_plugins() {
  41          $plugins = array_keys( yourls_get_plugins() );
  42          $expected = [
  43              '0' => 'test-plugin/plugin.php',
  44              '1' => 'test-plugin2/plugin.php',
  45          ];
  46          $this->assertSame($expected, $plugins);
  47      }
  48  
  49      /**
  50       * Check that a random "valid" plugin file validates as a plugin file
  51       */
  52  	public function test_plugin_validate() {
  53          $plugin = $this->pick_a_plugin();
  54          $this->assertTrue( yourls_is_a_plugin_file(YOURLS_PLUGINDIR . '/' . $plugin ) );
  55      }
  56  
  57      /**
  58       * Check that a nonexistent plugin file does not validate as a plugin file
  59       */
  60  	public function test_missing_plugin_validate() {
  61          $plugin = rand_str();
  62          $this->assertFalse( yourls_is_a_plugin_file(YOURLS_PLUGINDIR . '/' . $plugin ) );
  63      }
  64  
  65      /**
  66       * Check that an invalid plugin file does not validate as a plugin file
  67       *
  68       * @dataProvider get_invalid_code_plugins
  69       */
  70  	public function test_invalid_plugin_validate($plugin) {
  71          $this->assertFalse( yourls_is_a_plugin_file($plugin) );
  72      }
  73  
  74      /**
  75       * Check that a random valid plugin activates correctly
  76       */
  77  	public function test_plugin_activate() {
  78          $plugin = $this->pick_a_plugin();
  79  
  80          // Make sure the plugin.php is NOT present in get_included_files()
  81          // We sanitize the array to deal with different platforms (D:\hello\Windows vs /home/user/hello/Linux)
  82          $this->assertFalse(in_array(yourls_sanitize_filename(YOURLS_PLUGINDIR.'/'.$plugin),
  83              array_map('yourls_sanitize_filename', get_included_files())));
  84  
  85          // Activate the plugin
  86          $this->assertTrue( yourls_activate_plugin( $plugin ) );
  87          $this->assertGreaterThan( 0, yourls_has_active_plugins() );
  88          $this->assertTrue( yourls_is_active_plugin( $plugin ) );
  89  
  90          // Make sure the plugin.php is now present in get_included_files()
  91          $included_files = array_map('yourls_sanitize_filename', get_included_files()) ;
  92          $this->assertTrue(in_array(yourls_sanitize_filename(YOURLS_PLUGINDIR.'/'.$plugin), $included_files));
  93  
  94          // Make sure the plugin's uninstall.php is NOT present in get_included_files()
  95          $this->assertFalse(in_array(yourls_sanitize_filename(YOURLS_PLUGINDIR.'/'.dirname($plugin).'/uninstall.php'),$included_files));
  96  
  97          // We should NOT have YOURLS_UNINSTALL_PLUGIN defined
  98          $this->assertFalse(defined('YOURLS_UNINSTALL_PLUGIN'));
  99  
 100          return $plugin;
 101      }
 102  
 103      /**
 104       * Check that an active plugin does not activate
 105       *
 106       * @depends test_plugin_activate
 107       */
 108  	public function test_plugin_activate_twice( $plugin ) {
 109          $this->assertNotSame( true, yourls_activate_plugin( $plugin ) );
 110          // Note: we assertNotSame() with true because the function either returns true or a string
 111          return $plugin;
 112      }
 113  
 114      /**
 115      * Simulate initial plugin loading
 116      *
 117      * @depends test_plugin_activate
 118      */
 119      public function test_load_plugins( $plugin ) {
 120          $ydb = yourls_get_db();
 121  
 122          // at this point, we have exactly 1 plugin activated
 123          $this->assertSame( $ydb->get_plugins(), array( $plugin ) );
 124  
 125          // Register a nonexistent plugin to simulate one that was once activated but deleted since
 126          $fake_plugin = rand_str() . '/plugin.php';
 127          $ydb->add_plugin($fake_plugin);
 128  
 129          // Register a broken code plugin to simulate one that was once activated but now is broken
 130          $broken_plugin = $this->get_invalid_code_plugins()[ array_rand( $this->get_invalid_code_plugins() ) ][0];
 131          $ydb->add_plugin($broken_plugin);
 132  
 133          yourls_update_option( 'active_plugins', $ydb->get_plugins() );
 134  
 135          // Check we have 1 activated and 2 removed
 136          $load = yourls_load_plugins();
 137          $this->assertTrue( $load['loaded'] );
 138          $this->assertSame( $load['info'], '1 activated, 2 removed' );
 139  
 140          // Check only our valid plugin is left registered
 141          $this->assertSame( $ydb->get_plugins(), array( $plugin ) );
 142          $this->assertSame( 1, yourls_has_active_plugins() );
 143      }
 144  
 145      /**
 146       * Check that valid plugin deactivates correctly
 147       *
 148       * @depends test_plugin_activate
 149       */
 150  	public function test_plugin_deactivate( $plugin ) {
 151          $this->assertTrue( yourls_deactivate_plugin($plugin) );
 152          $this->assertSame( 0, yourls_has_active_plugins() );
 153          $this->assertFalse( yourls_is_active_plugin($plugin) );
 154          return $plugin;
 155      }
 156  
 157      /**
 158       * Check that deactivating a plugin correctly ran the uninstall script
 159       *
 160       * @depends test_plugin_deactivate
 161       */
 162      public function test_plugin_uninstall( $plugin ) {
 163          // Make sure uninstall.php is NOW present in get_included_files()
 164          $this->assertTrue( in_array( yourls_sanitize_filename(YOURLS_PLUGINDIR . '/' . dirname($plugin) . '/uninstall.php'),
 165          array_map('yourls_sanitize_filename', get_included_files())) );
 166  
 167          // we should now have YOURLS_UNINSTALL_PLUGIN set to true
 168          $this->assertTrue( defined('YOURLS_UNINSTALL_PLUGIN') && YOURLS_UNINSTALL_PLUGIN );
 169      }
 170  
 171      /**
 172       * Check that an missing plugin does not activate
 173       */
 174  	public function test_invalid_plugin_activate() {
 175          $plugin = rand_str();
 176  
 177          $this->assertNotSame( true, yourls_activate_plugin( $plugin ) );
 178          $this->assertFalse( yourls_is_active_plugin( $plugin ) );
 179      }
 180  
 181      /**
 182       * Check that a missing plugin does not deactivate
 183       */
 184  	public function test_invalid_plugin_deactivate() {
 185          $plugin = rand_str();
 186  
 187          $this->assertFalse( yourls_is_active_plugin( $plugin ) );
 188          $this->assertNotSame( true, yourls_deactivate_plugin( $plugin ) );
 189      }
 190  
 191      /**
 192       * Check that a plugin with invalid code does not activate
 193       * @dataProvider get_invalid_code_plugins
 194       */
 195      public function test_invalid_plugin_does_not_activate($plugin) {
 196          $this->assertNotTrue( yourls_activate_plugin_sandbox( $plugin ) );
 197          $this->assertNotTrue( yourls_activate_plugin( $plugin ) );
 198          $this->assertNotTrue( yourls_is_active_plugin( $plugin ) );
 199      }
 200  }


Generated: Wed Sep 28 05:10:02 2022 Cross-referenced by PHPXref 0.7.1