[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Plugins URLs and pages functions
   5   *
   6   * This test class should revert everything before each test: be cautious not to introduce
   7   * tests with a "depends" annotation
   8   *
   9   * @group plugins
  10   * @since 0.1
  11   */
  12  class Plugin_Pages_Tests extends PHPUnit\Framework\TestCase {
  13  
  14      protected function tearDown(): void {
  15          // remove filters and actions
  16          yourls_remove_all_filters( 'is_ssl' );
  17          yourls_remove_all_filters( 'needs_ssl' );
  18          yourls_remove_all_actions('pre_yourls_die');
  19  
  20          // unregister plugin pages
  21          yourls_get_db()->set_plugin_pages(array());
  22      }
  23  
  24      /**
  25      * Check that yourls_plugin_url() complies with SSL needs (1/4)
  26      *
  27      * @since 0.1
  28      */
  29      public function test_yourls_plugin_url_ssl_mode_1() {
  30          $plugin = rand_str();
  31  
  32          yourls_add_filter( 'is_ssl', 'yourls_return_false' );
  33          yourls_add_filter( 'needs_ssl', 'yourls_return_false' );
  34          $plugin_url = yourls_plugin_url( $plugin );
  35          $this->assertStringStartsWith( 'http://', $plugin_url );
  36      }
  37  
  38      /**
  39      * Check that yourls_plugin_url() complies with SSL needs (2/4)
  40      *
  41      * @since 0.1
  42      */
  43      public function test_yourls_plugin_url_ssl_mode_2() {
  44          $plugin = rand_str();
  45  
  46          yourls_add_filter( 'is_ssl', 'yourls_return_true' );
  47          yourls_add_filter( 'needs_ssl', 'yourls_return_false' );
  48          $plugin_url = yourls_plugin_url( $plugin );
  49          $this->assertStringStartsWith( 'https://', $plugin_url );
  50      }
  51  
  52      /**
  53      * Check that yourls_plugin_url() complies with SSL needs (3/4)
  54      *
  55      * @since 0.1
  56      */
  57      public function test_yourls_plugin_url_ssl_mode_3() {
  58          $plugin = rand_str();
  59  
  60          yourls_add_filter( 'is_ssl', 'yourls_return_true' );
  61          yourls_add_filter( 'needs_ssl', 'yourls_return_true' );
  62          $plugin_url = yourls_plugin_url( $plugin );
  63          $this->assertStringStartsWith( 'https://', $plugin_url );
  64      }
  65  
  66      /**
  67      * Check that yourls_plugin_url() complies with SSL needs (4/4)
  68      *
  69      * @since 0.1
  70      */
  71      public function test_yourls_plugin_url_ssl_mode_4() {
  72          $plugin = rand_str();
  73  
  74          yourls_add_filter( 'is_ssl', 'yourls_return_false' );
  75          yourls_add_filter( 'needs_ssl', 'yourls_return_true' );
  76          $plugin_url = yourls_plugin_url( $plugin );
  77          $this->assertStringStartsWith( 'https://', $plugin_url );
  78      }
  79  
  80      /**
  81      * Test registering a plugin page
  82      *
  83      * @since 0.1
  84      */
  85      public function test_register_plugin_page() {
  86          $ydb = yourls_get_db();
  87          $plugin = rand_str();
  88          $title = rand_str();
  89          $func = rand_str();
  90  
  91          // no plugin page registered
  92          $this->assertEquals( 0, count( $ydb->get_plugin_pages() ) );
  93  
  94          // register one and check
  95          yourls_register_plugin_page( $plugin, $title, $func );
  96          $this->assertEquals( 1, count( $ydb->get_plugin_pages() ) );
  97          $expected = array(
  98              'slug' => $plugin,
  99              'title' => $title,
 100              'function' => $func,
 101          );
 102          $pages = $ydb->get_plugin_pages();
 103          $this->assertSame( $pages[ $plugin ], $expected );
 104  
 105          // deregister it
 106          $ydb->set_plugin_pages(array());
 107      }
 108  
 109      /**
 110      * Check list of plugin admin pages
 111      *
 112      * @since 0.1
 113      */
 114      public function test_list_plugin_page() {
 115          $plugin = rand_str();
 116          $title = rand_str();
 117          $func = rand_str();
 118  
 119          // no plugin page registered
 120          $this->assertEmpty( yourls_list_plugin_admin_pages() );
 121  
 122          // register one plugin
 123          yourls_register_plugin_page( $plugin, $title, $func );
 124          $pages = yourls_list_plugin_admin_pages();
 125  
 126          $this->assertSame( 1, count( $pages ) );
 127          $this->assertArrayHasKey( 'url', $pages[ $plugin ] );
 128          $this->assertArrayHasKey( 'anchor', $pages[ $plugin ] );
 129      }
 130  
 131      /**
 132      * Simulate a non existent plugin admin page
 133      *
 134      * @since 0.1
 135      */
 136      public function test_plugin_admin_page_fake() {
 137          $this->expectException(Exception::class);
 138          $this->expectExceptionMessage('I have died');
 139  
 140          // intercept yourls_die() before it actually dies
 141          yourls_add_action( 'pre_yourls_die', function() { throw new Exception( 'I have died' ); } );
 142  
 143          // This should trigger yourls_die()
 144          yourls_plugin_admin_page( rand_str() );
 145      }
 146  
 147      /**
 148      * Simulate a valid plugin admin page
 149      *
 150      * @since 0.1
 151      */
 152      public function test_plugin_admin_page() {
 153          $plugin = rand_str();
 154          $title  = rand_str();
 155          $action = rand_str();
 156          $func = function() use ( $action ) { yourls_do_action( $action ); };
 157          yourls_register_plugin_page( $plugin, $title, $func );
 158  
 159          $this->assertSame( 0, yourls_did_action( 'load-' . $plugin ) );
 160  
 161          ob_start();
 162          yourls_plugin_admin_page( $plugin );
 163          ob_end_clean();
 164  
 165          // The page should have been drawn, and the plugin page callback should have been triggered
 166          $this->assertSame( 1, yourls_did_action( 'load-' . $plugin ) );
 167          $this->assertSame( 1, yourls_did_action( $action ) );
 168      }
 169  
 170      /**
 171      * Simulate a valid plugin admin page but with a drawing function that is not callable
 172      *
 173      * @since 0.1
 174      */
 175      public function test_plugin_admin_page_not_callable() {
 176          $this->expectException(Exception::class);
 177          $this->expectExceptionMessage('I have died');
 178  
 179          $plugin = rand_str();
 180          $title  = rand_str();
 181          $action = rand_str();
 182          $func   = rand_str();
 183          yourls_register_plugin_page( $plugin, $title, $func );
 184  
 185          $this->assertSame( 0, yourls_did_action( 'load-' . $plugin ) );
 186  
 187          // intercept yourls_die() before it actually dies
 188          yourls_add_action( 'pre_yourls_die', function() { throw new Exception( 'I have died' ); } );
 189  
 190          // This should trigger yourls_die()
 191          yourls_plugin_admin_page( $plugin );
 192      }
 193  
 194  }


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