Skip to content

Commit

Permalink
Added MkdirTaskTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sustmi committed Apr 16, 2017
1 parent bcfc162 commit a6b4356
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
128 changes: 128 additions & 0 deletions test/classes/phing/tasks/system/MkdirTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

require_once 'phing/BuildFileTest.php';

/**
* Tests the Mkdir Task
*
* @package phing.tasks.system
*/
class MkdirTaskTest extends BuildFileTest
{
private $originalUmask;

public function setUp()
{
$this->originalUmask = umask();
$this->configureProject(
PHING_TEST_BASE . '/etc/tasks/system/MkdirTaskTest.xml'
);

$this->executeTarget('clean');
mkdir(PHING_TEST_BASE . '/etc/tasks/system/tmp');
}

public function tearDown()
{
$this->executeTarget('clean');
umask($this->originalUmask);
}

/**
* @dataProvider umaskIsHonouredWhenNotUsingModeArgumentDataProvider
*/
public function testUmaskIsHonouredWhenNotUsingModeArgument($umask, $expectedDirMode)
{
umask($umask);
$this->executeTarget(__FUNCTION__);
$this->assertFileModeIs(PHING_TEST_BASE . '/etc/tasks/system/tmp/a', $expectedDirMode);
}

public function umaskIsHonouredWhenNotUsingModeArgumentDataProvider() {
return [
[0007, 0770],
[0077, 0700],
];
}

public function testUmaskIsIgnoredWhenUsingModeArgument()
{
umask(0077);
$this->executeTarget(__FUNCTION__);
$this->assertFileModeIs(PHING_TEST_BASE . '/etc/tasks/system/tmp/a', 0777);
}

public function testParentDirectoriesHaveDefaultPermissions()
{
umask(0077);
$this->executeTarget(__FUNCTION__);
$this->assertFileModeIs(PHING_TEST_BASE . '/etc/tasks/system/tmp/a', 0700);
$this->assertFileModeIs(PHING_TEST_BASE . '/etc/tasks/system/tmp/a/b', 0700);
$this->assertFileModeIs(PHING_TEST_BASE . '/etc/tasks/system/tmp/a/b/c', 0777);
}

public function testAclIsInheritedFromParentDirectoryDefaultAcl()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->markTestSkipped('POSIX ACL tests cannot be run on Windows.');
} else {
exec('which setfacl', $dummyOutput, $exitCode);
if ($exitCode !== 0) {
$this->markTestSkipped('"setfacl" command not found. POSIX ACL tests cannot be run.');
}
}

shell_exec('setfacl --remove-default ' . PHING_TEST_BASE . '/etc/tasks/system/tmp');
shell_exec('setfacl --modify default:user:root:rwx ' . PHING_TEST_BASE . '/etc/tasks/system/tmp');

$this->executeTarget(__FUNCTION__);

$this->assertFileAclContains(PHING_TEST_BASE . '/etc/tasks/system/tmp/a', 'user:root:rwx');
$this->assertFileAclContains(PHING_TEST_BASE . '/etc/tasks/system/tmp/a', 'default:user:root:rwx');
}

/**
* @param string $filename
* @param int $mode
*/
private function assertFileModeIs($filename, $mode)
{
$stat = stat($filename);

$this->assertSame(
sprintf("%03o", $mode),
sprintf("%03o", $stat['mode'] & 0777),
sprintf('Failed asserting that file mode of "%s" is %03o', $filename, $mode)
);
}

/**
* @param string $filename
* @param string $expectedAclEntry
*/
private function assertFileAclContains($filename, $expectedAclEntry)
{
$output = shell_exec('getfacl --omit-header --no-effective --absolute-names ' . escapeshellarg($filename));

$aclEntries = preg_split('/[\r\n]+/', $output, -1, PREG_SPLIT_NO_EMPTY);

$matchFound = false;
foreach ($aclEntries as $aclEntry) {
if ($aclEntry === $expectedAclEntry) {
$matchFound = true;
break;
}
}

$this->assertTrue(
$matchFound,
sprintf(
'Failed asserting that ACL of file "%s" contains "%s" entry.' . "\n"
. 'Following ACL entries are present:' . "\n%s\n",
$filename,
$expectedAclEntry,
implode("\n", $aclEntries)
)
);
}
}
24 changes: 24 additions & 0 deletions test/etc/tasks/system/MkdirTaskTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="MkdirTaskTest" default="main">
<property name="tmp.dir" value="tmp"/>

<target name="clean">
<delete dir="${tmp.dir}"/>
</target>

<target name="testUmaskIsHonouredWhenNotUsingModeArgument">
<mkdir dir="${tmp.dir}/a" />
</target>

<target name="testUmaskIsIgnoredWhenUsingModeArgument">
<mkdir dir="${tmp.dir}/a" mode="0777" />
</target>

<target name="testParentDirectoriesHaveDefaultPermissions">
<mkdir dir="${tmp.dir}/a/b/c" mode="0777" />
</target>

<target name="testAclIsInheritedFromParentDirectoryDefaultAcl">
<mkdir dir="${tmp.dir}/a" />
</target>
</project>

0 comments on commit a6b4356

Please sign in to comment.