1<?php
2
3class init_creationmodes_test extends DokuWikiTest
4{
5
6    protected $oldumask;
7    protected $dir;
8    protected $file;
9
10    /** @inheritDoc */
11    public static function setUpBeforeClass(): void
12    {
13        parent::setUpBeforeClass();
14        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
15            self::markTestSkipped('Permission checks skipped on Windows');
16        }
17    }
18
19    /**
20     * set up the file and directory we use for testing
21     */
22    protected function init()
23    {
24        $this->dir = getCacheName('dir', '.creationmode_test');
25        $this->file = getCacheName('bar', '.creationmode_test');
26    }
27
28    /** @inheritDoc */
29    public function setUp(): void
30    {
31        parent::setUp();
32
33        if (!isset($this->dir)) $this->init();
34        $this->oldumask = umask();
35    }
36
37    /** @inheritDoc */
38    protected function tearDown(): void
39    {
40        umask($this->oldumask);
41
42        chmod($this->dir, 0777);
43        rmdir($this->dir);
44
45        chmod($this->file, 0777);
46        unlink($this->file);
47
48        parent::tearDown();
49
50    }
51
52    /**
53     * @return Generator|string[]
54     * @see testFilemodes
55     */
56    public function provideFilemodes()
57    {
58        $umasks = [0000, 0022, 0002, 0007];
59        $fmodes = [0777, 0666, 0644, 0640, 0664, 0660];
60        $dmodes = [0777, 0775, 0755, 0750, 0770, 0700];
61
62        foreach ($umasks as $umask) {
63            foreach ($dmodes as $dmode) {
64                foreach ($fmodes as $fmode) {
65                    yield [$umask, $dmode, $fmode];
66                }
67            }
68        }
69    }
70
71    /**
72     * @dataProvider provideFilemodes
73     */
74    public function testFilemodes($umask, $dmode, $fmode)
75    {
76        global $conf;
77
78        // setup
79        $conf['dmode'] = $dmode;
80        $conf['fmode'] = $fmode;
81        umask($umask);
82
83        // create
84        init_creationmodes();
85        io_mkdir_p($this->dir);
86        io_saveFile($this->file, 'test');
87
88        // get actual values (removing the status bits)
89        clearstatcache();
90        $dperm = fileperms($this->dir) - 0x4000;
91        $fperm = fileperms($this->file) - 0x8000;
92
93
94        $this->assertSame($dmode, $dperm,
95            sprintf(
96                'dir had %04o, expected %04o with umask %04o (fperm: %04o)',
97                $dperm, $dmode, $umask, $conf['dperm']
98            )
99        );
100        $this->assertSame($fmode, $fperm,
101            sprintf(
102                'file had %04o, expected %04o with umask %04o (fperm: %04o)',
103                $fperm, $fmode, $umask, $conf['fperm']
104            )
105        );
106    }
107
108}
109
110