Passed
Push — master ( 972120...1c77fc )
by Michiel
08:19
created

ChownTaskTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 3 1
A setUp() 0 4 1
B testChangeGroup() 0 43 6
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\Tasks\System;
21
22
use Phing\Support\BuildFileTest;
23
24
/**
25
 * Tests the Chown Task
26
 *
27
 * @author  Michiel Rook <[email protected]>
28
 * @package phing.tasks.system
29
 *
30
 * @requires OS ^(?:(?!Win).)*$
31
 */
32
class ChownTaskTest extends BuildFileTest
33
{
34
    public function setUp(): void
35
    {
36
        $this->configureProject(
37
            PHING_TEST_BASE . '/etc/tasks/system/ChownTaskTest.xml'
38
        );
39
    }
40
41
    public function tearDown(): void
42
    {
43
        $this->executeTarget('clean');
44
    }
45
46
    public function testChangeGroup()
47
    {
48
        $userinfo = posix_getpwuid(posix_geteuid());
49
        $username = $userinfo['name'];
50
51
        //we may change the group only if we belong to it
52
        //so find a group that we are in
53
        $group = null;
54
        foreach (['users', 'www-data', 'cdrom'] as $groupname) {
55
            $grpinfo = posix_getgrnam($groupname);
56
            if ($grpinfo['gid'] == $userinfo['gid']) {
57
                //current group id, the file has that group anyway
58
                continue;
59
            }
60
            if (!is_array($grpinfo['members'])) {
61
                continue;
62
            }
63
            if (in_array($username, $grpinfo['members'])) {
64
                $group = $grpinfo;
65
                break;
66
            }
67
        }
68
        if ($group === null) {
69
            $this->markTestSkipped('found no group we can change ownership to');
70
        }
71
72
        $this->project->setUserProperty(
73
            'targetuser',
74
            $username . '.' . $group['name']
75
        );
76
        $this->executeTarget(__FUNCTION__);
77
        $a = stat(PHING_TEST_BASE . '/etc/tasks/system/tmp/chowntestA');
78
        $b = stat(PHING_TEST_BASE . '/etc/tasks/system/tmp/chowntestB');
79
80
        $this->assertNotEquals(
81
            $group['gid'],
82
            $a['gid'],
83
            'chowntestA group should not have changed'
84
        );
85
        $this->assertEquals(
86
            $group['gid'],
87
            $b['gid'],
88
            'chowntestB group should have changed'
89
        );
90
    }
91
}
92