ORIENT/tests/unit/cms/classes/CmsObjectTest.php

339 lines
10 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php
use Cms\Classes\CmsObject;
use Cms\Classes\Theme;
class TestCmsObject extends CmsObject
{
protected $dirName = 'testobjects';
2014-05-14 13:24:20 +00:00
}
class TestTemporaryCmsObject extends CmsObject
{
protected $dirName = 'temporary';
2014-05-14 13:24:20 +00:00
}
class CmsObjectTest extends TestCase
{
public function testLoad()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('test');
2014-05-14 13:24:20 +00:00
$obj = TestCmsObject::load($theme, 'plain.html');
$this->assertEquals('<p>This is a test HTML content file.</p>', $obj->getContent());
$this->assertEquals('plain.html', $obj->getFileName());
$path = $theme->getPath().'/testobjects/plain.html';
$this->assertEquals($path, $obj->getFilePath());
2014-05-14 13:24:20 +00:00
$this->assertEquals(filemtime($path), $obj->mtime);
}
public function testLoadFromSubdirectory()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('test');
2014-05-14 13:24:20 +00:00
$obj = TestCmsObject::load($theme, 'subdir/obj.html');
$this->assertEquals('<p>This is an object in a subdirectory.</p>', $obj->getContent());
$this->assertEquals('subdir/obj.html', $obj->getFileName());
$path = $theme->getPath().'/testobjects/subdir/obj.html';
$this->assertEquals($path, $obj->getFilePath());
2014-05-14 13:24:20 +00:00
$this->assertEquals(filemtime($path), $obj->mtime);
}
public function testValidateLoadInvalidTheme()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('none');
2014-05-14 13:24:20 +00:00
$this->assertNull(TestCmsObject::load($theme, 'plain.html'));
}
public function testValidateLoadInvalidFile()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('test');
2014-05-14 13:24:20 +00:00
$this->assertNull(TestCmsObject::load($theme, 'none'));
}
public function testCache()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('test');
2014-05-14 13:24:20 +00:00
$themePath = $theme->getPath();
$filePath = $themePath .= '/temporary/test.htm';
if (file_exists($filePath)) {
2014-05-14 13:24:20 +00:00
@unlink($filePath);
}
2014-05-14 13:24:20 +00:00
$this->assertFileNotExists($filePath);
file_put_contents($filePath, '<p>Test content</p>');
/*
* First try - the object should be loaded from the file
*/
$obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
$this->assertFalse($obj->isLoadedFromCache());
$this->assertEquals('<p>Test content</p>', $obj->getContent());
$this->assertEquals('test.htm', $obj->getFileName());
$this->assertEquals(filemtime($filePath), $obj->mtime);
/*
* Second try - the object should be loaded from the cache
*/
CmsObject::clearInternalCache();
$obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
$this->assertTrue($obj->isLoadedFromCache());
$this->assertEquals('<p>Test content</p>', $obj->getContent());
$this->assertEquals('test.htm', $obj->getFileName());
$this->assertEquals(filemtime($filePath), $obj->mtime);
/*
* Modify the file. The object should be loaded from the disk and re-cached.
*/
sleep(1); // Sleep a second in order to have the update file modification time
file_put_contents($filePath, '<p>Updated test content</p>');
clearstatcache(); // The filemtime() function caches its value within a request, so we should clear its cache.
CmsObject::clearInternalCache();
$obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
$this->assertFalse($obj->isLoadedFromCache());
$this->assertEquals('<p>Updated test content</p>', $obj->getContent());
$this->assertEquals(filemtime($filePath), $obj->mtime);
CmsObject::clearInternalCache();
$obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
$this->assertTrue($obj->isLoadedFromCache());
$this->assertEquals('<p>Updated test content</p>', $obj->getContent());
$this->assertEquals(filemtime($filePath), $obj->mtime);
/*
* Delete the file. The loadCached() should return null
*/
@unlink($filePath);
$this->assertFileNotExists($filePath);
CmsObject::clearInternalCache();
$obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
$this->assertNull($obj);
}
public function testFillFillable()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => 'mytestobj',
'content' => $testContents
2014-05-14 13:24:20 +00:00
]);
$this->assertEquals($testContents, $obj->getContent());
$this->assertEquals('mytestobj.htm', $obj->getFileName());
}
public function testFillNotFillable()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'something' => 'mytestobj',
'content' => $testContents
2014-05-14 13:24:20 +00:00
]);
$this->assertNull($obj->something);
2014-05-14 13:24:20 +00:00
}
public function testFillInvalidFileNameSymbol()
{
$this->expectException(\October\Rain\Exception\ValidationException::class);
$this->expectExceptionMessage('Invalid file name');
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => '@name'
2014-05-14 13:24:20 +00:00
]);
$obj->save();
2014-05-14 13:24:20 +00:00
}
public function testFillInvalidFileNamePath()
{
$this->expectException(\October\Rain\Exception\ValidationException::class);
$this->expectExceptionMessage('Invalid file name');
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => '../somefile'
2014-05-14 13:24:20 +00:00
]);
$obj->save();
2014-05-14 13:24:20 +00:00
}
public function testFillInvalidFileSlash()
{
$this->expectException(\October\Rain\Exception\ValidationException::class);
$this->expectExceptionMessage('Invalid file name');
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => '/somefile'
2014-05-14 13:24:20 +00:00
]);
$obj->save();
2014-05-14 13:24:20 +00:00
}
public function testFillEmptyFileName()
{
$this->expectException(\October\Rain\Exception\ValidationException::class);
$this->expectExceptionMessage('The File Name field is required');
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => ' '
2014-05-14 13:24:20 +00:00
]);
$obj->save();
2014-05-14 13:24:20 +00:00
}
public function testSave()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$destFilePath = $theme->getPath().'/testobjects/mytestobj.htm';
if (file_exists($destFilePath)) {
2014-05-14 13:24:20 +00:00
unlink($destFilePath);
}
2014-05-14 13:24:20 +00:00
$this->assertFileNotExists($destFilePath);
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => 'mytestobj',
'content' => $testContents
2014-05-14 13:24:20 +00:00
]);
$obj->save();
$this->assertFileExists($destFilePath);
$this->assertEquals($testContents, file_get_contents($destFilePath));
}
/**
* @depends testSave
*/
public function testRename()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$srcFilePath = $theme->getPath().'/testobjects/mytestobj.htm';
$this->assertFileExists($srcFilePath);
$destFilePath = $theme->getPath().'/testobjects/anotherobj.htm';
if (file_exists($destFilePath)) {
2014-05-14 13:24:20 +00:00
unlink($destFilePath);
}
2014-05-14 13:24:20 +00:00
$testContents = 'mytestcontent';
$obj = TestCmsObject::load($theme, 'mytestobj.htm');
$this->assertEquals($testContents, $obj->getContent());
$obj->fill([
'fileName' => 'anotherobj'
2014-05-14 13:24:20 +00:00
]);
$obj->save();
$this->assertFileNotExists($srcFilePath);
$this->assertFileExists($destFilePath);
$this->assertEquals($testContents, file_get_contents($destFilePath));
}
/**
* @depends testRename
*/
public function testRenameToExistingFile()
{
$this->expectException(\October\Rain\Exception\ApplicationException::class);
$this->expectExceptionMessageMatches('/already\sexists/');
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$srcFilePath = $theme->getPath().'/testobjects/anotherobj.htm';
$this->assertFileExists($srcFilePath);
$destFilePath = $theme->getPath().'/testobjects/existingobj.htm';
if (!file_exists($destFilePath)) {
2014-05-14 13:24:20 +00:00
file_put_contents($destFilePath, 'str');
}
2014-05-14 13:24:20 +00:00
$this->assertFileExists($destFilePath);
$obj = TestCmsObject::load($theme, 'anotherobj.htm');
$obj->fill(['fileName' => 'existingobj']);
2014-05-14 13:24:20 +00:00
$obj->save();
}
/**
* @depends testRename
*/
public function testSaveSameName()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$filePath = $theme->getPath().'/testobjects/anotherobj.htm';
$this->assertFileExists($filePath);
$testContents = 'new content';
$obj = TestCmsObject::load($theme, 'anotherobj.htm');
$obj->fill([
'fileName' => 'anotherobj',
'content' => $testContents
2014-05-14 13:24:20 +00:00
]);
$obj->save();
$this->assertFileExists($filePath);
$this->assertEquals($testContents, file_get_contents($filePath));
}
public function testSaveNewDir()
{
2014-12-10 06:42:50 +00:00
$theme = Theme::load('apitest');
2014-05-14 13:24:20 +00:00
$destFilePath = $theme->getPath().'/testobjects/testsubdir/mytestobj.htm';
if (file_exists($destFilePath)) {
2014-05-14 13:24:20 +00:00
unlink($destFilePath);
}
2014-05-14 13:24:20 +00:00
$destDirPath = dirname($destFilePath);
if (file_exists($destDirPath) && is_dir($destDirPath)) {
2014-05-14 13:24:20 +00:00
rmdir($destDirPath);
}
2014-05-14 13:24:20 +00:00
$this->assertFileNotExists($destFilePath);
$this->assertFileNotExists($destDirPath);
$testContents = 'mytestcontent';
$obj = TestCmsObject::inTheme($theme);
2014-05-14 13:24:20 +00:00
$obj->fill([
'fileName' => 'testsubdir/mytestobj.htm',
'content' => $testContents
2014-05-14 13:24:20 +00:00
]);
$obj->save();
$this->assertFileExists($destFilePath);
$this->assertEquals($testContents, file_get_contents($destFilePath));
}
}