[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/install/ -> htaccess.php (source)

   1  <?php
   2  
   3  /**
   4   * Test htaccess creation & modification functions
   5   *
   6   * @group install
   7   */
   8  class Install_htaccess_Tests extends PHPUnit\Framework\TestCase {
   9  
  10      protected $server;
  11  
  12      /**
  13       * Make a copy of $_SERVER
  14       */
  15      public function setUp(): void {
  16          $this->server = $_SERVER;
  17      }
  18  
  19      /**
  20       * Restore original $_SERVER
  21       */
  22      public function tearDown(): void {
  23          $_SERVER = $this->server;
  24      }
  25  
  26      /**
  27       * Provide server signatures, wether they're Apache (true) or something else (false) and
  28       * the name of the redirect rule file (.htaccess or web.config)
  29       */
  30      public function servers() {
  31          return array(
  32              array( 'Very Common Apache', true,  '.htaccess'  ),
  33              array( 'LiteSpeed So Fast',  true,  '.htaccess'  ),
  34              array( 'meh IIS meh',        false, 'web.config' ),
  35          );
  36      }
  37  
  38      /**
  39       * Check .htaccess creation - general function, checking if file is created
  40       *
  41       * @dataProvider servers
  42       * @since 0.1
  43       */
  44  	public function test_htaccess( $server, $is_apache, $file ) {
  45          $_SERVER['SERVER_SOFTWARE'] = $server;
  46  
  47          $this->assertSame( $is_apache, yourls_is_apache() );
  48  
  49          if( file_exists( YOURLS_ABSPATH . '/' . $file ) )
  50              @unlink( YOURLS_ABSPATH . '/' . $file );
  51  
  52          $this->assertTrue( yourls_create_htaccess() );
  53          $this->assertFileExists( YOURLS_ABSPATH . '/' . $file );
  54      }
  55  
  56      /**
  57       * Files in which we want to insert content
  58       */
  59      public function htaccess_content() {
  60          return array(
  61              array( 'original_nofile.txt' ),
  62              array( 'original_empty.txt' ),
  63              array( 'original_fresh-YOURLS-content.txt' ),
  64              array( 'original_old-YOURLS-content.txt' ),
  65              array( 'original_other-content.txt' ),
  66          );
  67      }
  68  
  69      /**
  70       * Check .htaccess creation - specific cases, checking file contents
  71       *
  72       * @dataProvider htaccess_content
  73       * @since 0.1
  74       */
  75  	public function test_htaccess_content( $filename ) {
  76          $newfile  = str_replace( 'original_', 'test_', $filename );
  77          $expected = str_replace( 'original_', 'expected_', $filename );
  78  
  79          $newfile  = YOURLS_TESTDATA_DIR . '/htaccess/' . $newfile;
  80          $filename = YOURLS_TESTDATA_DIR . '/htaccess/' . $filename;
  81          $expected = YOURLS_TESTDATA_DIR . '/htaccess/' . $expected;
  82  
  83          // If file exist, copy it (if file doesn't exist, it's because we're creating from scratch)
  84          if( file_exists( $filename ) ) {
  85              if ( !copy( $filename, $newfile ) ) {
  86                  $this->markTestSkipped( "Cannot copy file $filename" );
  87              }
  88          }
  89  
  90          $marker = 'YOURLS';
  91          $content = array(
  92              'This is a test',
  93              'Hello World',
  94              '1,2... 1,2...',
  95          );
  96  
  97          // check we can create the htaccess
  98          $this->assertTrue( yourls_insert_with_markers( $newfile, $marker, $content ) );
  99  
 100          // check it is correctly create. First, remove line endings so tests are consistent between Windows and Linux
 101          // For this reason, we're not using $this->assertFileEquals()
 102          $exp = array_map( function($line) {return str_replace(array("\r", "\n"), '', $line);}, file($expected));
 103          $new = array_map( function($line) {return str_replace(array("\r", "\n"), '', $line);}, file($newfile));
 104          $this->assertSame($exp, $new);
 105          unlink( $newfile );
 106      }
 107  
 108  }


Generated: Mon Mar 3 05:10:02 2025 Cross-referenced by PHPXref 0.7.1