silverstripe-framework/tests/filesystem/AssetAdapterTest.php

76 lines
2.7 KiB
PHP

<?php
use SilverStripe\Assets\Flysystem\ProtectedAssetAdapter;
use SilverStripe\Assets\Flysystem\PublicAssetAdapter;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Assets\File;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Core\Config\Config;
class AssetAdapterTest extends SapphireTest {
protected $rootDir = null;
protected $originalServer = null;
public function setUp() {
parent::setUp();
$this->rootDir = ASSETS_PATH . '/AssetAdapterTest';
Filesystem::makeFolder($this->rootDir);
Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', '/');
$this->originalServer = $_SERVER;
}
public function tearDown() {
if($this->rootDir) {
Filesystem::removeFolder($this->rootDir);
$this->rootDir = null;
}
if($this->originalServer) {
$_SERVER = $this->originalServer;
$this->originalServer = null;
}
parent::tearDown();
}
public function testPublicAdapter() {
$_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Win64) PHP/5.3.13';
$adapter = new PublicAssetAdapter($this->rootDir);
$this->assertFileExists($this->rootDir . '/.htaccess');
$this->assertFileNotExists($this->rootDir . '/web.config');
$htaccess = $adapter->read('.htaccess');
$content = $htaccess['contents'];
// Allowed extensions set
$this->assertContains('RewriteCond %{REQUEST_URI} !\\.(?i:', $content);
foreach(File::config()->allowed_extensions as $extension) {
$this->assertRegExp('/\b'.preg_quote($extension).'\b/', $content);
}
// Rewrite rules
$this->assertContains('RewriteRule .* ../framework/main.php?url=%1 [QSA]', $content);
$this->assertContains('RewriteRule error[^\\\\/]*\\.html$ - [L]', $content);
// Test flush restores invalid content
\file_put_contents($this->rootDir . '/.htaccess', '# broken content');
$adapter->flush();
$htaccess2 = $adapter->read('.htaccess');
$this->assertEquals($content, $htaccess2['contents']);
// Test URL
$this->assertEquals('/assets/AssetAdapterTest/file.jpg', $adapter->getPublicUrl('file.jpg'));
}
public function testProtectedAdapter() {
$_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Win64) PHP/5.3.13';
$adapter = new ProtectedAssetAdapter($this->rootDir . '/.protected');
$this->assertFileExists($this->rootDir . '/.protected/.htaccess');
$this->assertFileNotExists($this->rootDir . '/.protected/web.config');
// Test url
$this->assertEquals('/assets/file.jpg', $adapter->getProtectedUrl('file.jpg'));
}
}