Skip to content

Commit

Permalink
BUG Fixed cross-platform issues with test cases and file utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
tractorcow committed Oct 4, 2013
1 parent 1a002b7 commit f67b549
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
8 changes: 4 additions & 4 deletions core/TempPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function getTempParentFolder($base = null) {
$worked = true;

// first, try finding a silverstripe-cache dir built off the base path
$tempPath = $base . '/silverstripe-cache';
$tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache';
if(@file_exists($tempPath)) {
if((fileperms($tempPath) & 0777) != 0777) {
@chmod($tempPath, 0777);
Expand All @@ -55,7 +55,7 @@ function getTempParentFolder($base = null) {
}

// failing the above, try finding a namespaced silverstripe-cache dir in the system temp
$cacheFolder = '/silverstripe-cache' . str_replace(array(' ', '/', ':', '\\'), '-', $base);
$cacheFolder = DIRECTORY_SEPARATOR . 'silverstripe-cache' . str_replace(array(' ', '/', ':', '\\'), '-', $base);
$tempPath = sys_get_temp_dir() . $cacheFolder;
if(!@file_exists($tempPath)) {
$oldUMask = umask(0);
Expand All @@ -72,7 +72,7 @@ function getTempParentFolder($base = null) {
// failing to use the system path, attempt to create a local silverstripe-cache dir
if(!$worked) {
$worked = true;
$tempPath = $base . '/silverstripe-cache';
$tempPath = $base . DIRECTORY_SEPARATOR . 'silverstripe-cache';
if(!@file_exists($tempPath)) {
$oldUMask = umask(0);
$worked = @mkdir($tempPath, 0777);
Expand All @@ -89,4 +89,4 @@ function getTempParentFolder($base = null) {
}

return $tempPath;
}
}
36 changes: 19 additions & 17 deletions tests/core/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,48 @@ class CoreTest extends SapphireTest {

public function setUp() {
parent::setUp();
$this->tempPath = Director::baseFolder() . '/silverstripe-cache';
$this->tempPath = Director::baseFolder() . DIRECTORY_SEPARATOR . 'silverstripe-cache';
}

public function testGetTempPathInProject() {
$user = getTempFolderUsername();

if(file_exists($this->tempPath)) {
$this->assertEquals(getTempFolder(BASE_PATH), $this->tempPath);
$this->assertEquals(getTempFolder(BASE_PATH), $this->tempPath . DIRECTORY_SEPARATOR . $user);
} else {
$user = getTempFolderUsername();

// A typical Windows location for where sites are stored on IIS
$this->assertEquals(sys_get_temp_dir() .
'/silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project/' . $user,
$this->assertEquals(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project' . DIRECTORY_SEPARATOR . $user,
getTempFolder('C:\\inetpub\\wwwroot\\silverstripe-test-project'));

// A typical Mac OS X location for where sites are stored
$this->assertEquals(sys_get_temp_dir() .
'/silverstripe-cache-Users-joebloggs-Sites-silverstripe-test-project/' . $user,
$this->assertEquals(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'silverstripe-cache-Users-joebloggs-Sites-silverstripe-test-project' . DIRECTORY_SEPARATOR . $user,
getTempFolder('/Users/joebloggs/Sites/silverstripe-test-project'));

// A typical Linux location for where sites are stored
$this->assertEquals(sys_get_temp_dir() . '/silverstripe-cache-var-www-silverstripe-test-project/' . $user,
$this->assertEquals(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'silverstripe-cache-var-www-silverstripe-test-project' . DIRECTORY_SEPARATOR . $user,
getTempFolder('/var/www/silverstripe-test-project'));
}
}

public function tearDown() {
parent::tearDown();
$user = getTempFolderUsername();
if(file_exists(sys_get_temp_dir() . '/silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project')) {
rmdir(sys_get_temp_dir() . '/silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project/' . $user);
rmdir(sys_get_temp_dir() . '/silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project');
foreach(array(
'silverstripe-cacheC--inetpub-wwwroot-silverstripe-test-project',
'silverstripe-cache-Users-joebloggs-Sites-silverstripe-test-project',
'silverstripe-cache-var-www-silverstripe-test-project'
) as $dir) {
$path = sys_get_temp_dir().DIRECTORY_SEPARATOR.$dir;
if(file_exists($path)) {
rmdir($path . DIRECTORY_SEPARATOR . $user);
rmdir($path);
}
if(file_exists(sys_get_temp_dir() . '/silverstripe-cache-Users-joebloggs-Sites-silverstripe-test-project')) {
rmdir(sys_get_temp_dir() . '/silverstripe-cache-Users-joebloggs-Sites-silverstripe-test-project/' . $user);
rmdir(sys_get_temp_dir() . '/silverstripe-cache-Users-joebloggs-Sites-silverstripe-test-project');
}
if(file_exists(sys_get_temp_dir() . '/silverstripe-cache-var-www-silverstripe-test-project')) {
rmdir(sys_get_temp_dir() . '/silverstripe-cache-var-www-silverstripe-test-project/' . $user);
rmdir(sys_get_temp_dir() . '/silverstripe-cache-var-www-silverstripe-test-project');
}
}

}
40 changes: 21 additions & 19 deletions tests/i18n/i18nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class i18nTest extends SapphireTest {
public function setUp() {
parent::setUp();

$this->alternateBasePath = $this->getCurrentAbsolutePath() . "/_fakewebroot";
$this->alternateBaseSavePath = TEMP_FOLDER . '/i18nTextCollectorTest_webroot';
$this->alternateBasePath = $this->getCurrentAbsolutePath() . DIRECTORY_SEPARATOR . "_fakewebroot";
$this->alternateBaseSavePath = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'i18nTextCollectorTest_webroot';
FileSystem::makeFolder($this->alternateBaseSavePath);
Director::setBaseFolder($this->alternateBasePath);

Expand Down Expand Up @@ -182,13 +182,13 @@ public function testTemplateTranslation() {
), 'en_US');

$viewer = new SSViewer('i18nTestModule');
$parsedHtml = $viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue')));
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue'))));
$this->assertContains(
"Layout Template\n",
Convert::nl2os("Layout Template\n"),
$parsedHtml
);
$this->assertContains(
"Layout Template no namespace\n",
Convert::nl2os("Layout Template no namespace\n"),
$parsedHtml
);

Expand All @@ -206,37 +206,37 @@ public function testTemplateTranslation() {
), 'de_DE');

$viewer = new SSViewer('i18nTestModule');
$parsedHtml = $viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue')));
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue'))));
$this->assertContains(
"TRANS Main Template\n",
Convert::nl2os("TRANS Main Template\n"),
$parsedHtml
);
$this->assertContains(
"TRANS Layout Template\n",
Convert::nl2os("TRANS Layout Template\n"),
$parsedHtml
);
$this->assertContains(
"TRANS Layout Template no namespace",
Convert::nl2os("TRANS Layout Template no namespace\n"),
$parsedHtml
);
$this->assertContains(
"TRANS My replacement: TestPropertyValue",
Convert::nl2os("TRANS My replacement: TestPropertyValue\n"),
$parsedHtml
);
$this->assertContains(
"TRANS Include Entity with Namespace",
Convert::nl2os("TRANS Include Entity with Namespace\n"),
$parsedHtml
);
$this->assertContains(
"TRANS Include Entity without Namespace",
Convert::nl2os("TRANS Include Entity without Namespace\n"),
$parsedHtml
);
$this->assertContains(
"TRANS My include replacement: TestPropertyValue",
Convert::nl2os("TRANS My include replacement: TestPropertyValue\n"),
$parsedHtml
);
$this->assertContains(
"TRANS My include replacement no namespace: TestPropertyValue",
Convert::nl2os("TRANS My include replacement no namespace: TestPropertyValue\n"),
$parsedHtml
);

Expand Down Expand Up @@ -335,25 +335,27 @@ public function testNewTemplateTranslation() {
),'en_US');

$viewer = new SSViewer('i18nTestModule');
$parsedHtml = $viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue')));
$parsedHtml = Convert::nl2os($viewer->process(new ArrayData(array('TestProperty' => 'TestPropertyValue'))));
$this->assertContains(
"Hello Mark welcome. But it is late, bye\n",
Convert::nl2os("Hello Mark welcome. But it is late, bye\n"),
$parsedHtml, "Testing fallback to the translation default (but using the injection array)"
);

$this->assertContains(
"TRANS Hello Paul good you are here. But it is late, see you\n",
Convert::nl2os("TRANS Hello Paul good you are here. But it is late, see you\n"),
$parsedHtml, "Testing entity, default string and injection array"
);

$this->assertContains(
"TRANS Hello Cat meow. But it is late, meow\n",
Convert::nl2os("TRANS Hello Cat meow. But it is late, meow\n"),
$parsedHtml, "Testing a translation with just entity and injection array"
);

//test injected calls
$this->assertContains(
"TRANS Hello ".Director::absoluteBaseURL()." ".i18n::get_locale().". But it is late, global calls\n",
Convert::nl2os(
"TRANS Hello ".Director::absoluteBaseURL()." ".i18n::get_locale().". But it is late, global calls\n"
),
$parsedHtml,
"Testing a translation with just entity and injection array, but with global variables injected in"
);
Expand Down

0 comments on commit f67b549

Please sign in to comment.