Passed
Push — main ( 2c83d5...fb3f8a )
by Siad
05:21
created

ChownTaskTest::testChangeGroup()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 43
rs 8.8497
c 0
b 0
f 0
cc 6
nc 10
nop 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Test\Task\System;
21
22
use Phing\Test\Support\BuildFileTest;
23
24
/**
25
 * Tests the Chown Task
26
 *
27
 * @author  Michiel Rook <[email protected]>
28
 *
29
 * @requires OS ^(?:(?!Win).)*$
30
 */
31
class ChownTaskTest extends BuildFileTest
32
{
33
    public function setUp(): void
34
    {
35
        $this->configureProject(
36
            PHING_TEST_BASE . '/etc/tasks/system/ChownTaskTest.xml'
37
        );
38
    }
39
40
    public function tearDown(): void
41
    {
42
        $this->executeTarget('clean');
43
    }
44
45
    public function testChangeGroup()
46
    {
47
        $userinfo = posix_getpwuid(posix_geteuid());
48
        $username = $userinfo['name'];
49
50
        //we may change the group only if we belong to it
51
        //so find a group that we are in
52
        $group = null;
53
        foreach (['users', 'www-data', 'cdrom'] as $groupname) {
54
            $grpinfo = posix_getgrnam($groupname);
55
            if ($grpinfo['gid'] == $userinfo['gid']) {
56
                //current group id, the file has that group anyway
57
                continue;
58
            }
59
            if (!is_array($grpinfo['members'])) {
60
                continue;
61
            }
62
            if (in_array($username, $grpinfo['members'])) {
63
                $group = $grpinfo;
64
                break;
65
            }
66
        }
67
        if ($group === null) {
68
            $this->markTestSkipped('found no group we can change ownership to');
69
        }
70
71
        $this->project->setUserProperty(
72
            'targetuser',
73
            $username . '.' . $group['name']
74
        );
75
        $this->executeTarget(__FUNCTION__);
76
        $a = stat(PHING_TEST_BASE . '/etc/tasks/system/tmp/chowntestA');
77
        $b = stat(PHING_TEST_BASE . '/etc/tasks/system/tmp/chowntestB');
78
79
        $this->assertNotEquals(
80
            $group['gid'],
81
            $a['gid'],
82
            'chowntestA group should not have changed'
83
        );
84
        $this->assertEquals(
85
            $group['gid'],
86
            $b['gid'],
87
            'chowntestB group should have changed'
88
        );
89
    }
90
}
91