xref: /plugin/farmer/_test/helper.test.php (revision 5b5c8486c2fd499fa1aa5be2b0fd2e2fcb622f06)
178c63d53SAndreas Gohr<?php
278c63d53SAndreas Gohr
378c63d53SAndreas Gohr/**
478c63d53SAndreas Gohr * Tests for the validation functionality of the farmer plugin
578c63d53SAndreas Gohr *
678c63d53SAndreas Gohr * @group plugin_farmer
778c63d53SAndreas Gohr * @group plugins
878c63d53SAndreas Gohr */
9*5b5c8486SAndreas Gohrclass helper_plugin_farmer_test extends DokuWikiTest
10*5b5c8486SAndreas Gohr{
1178c63d53SAndreas Gohr
12*5b5c8486SAndreas Gohr    protected $pluginsEnabled = ['farmer',];
1378c63d53SAndreas Gohr
14*5b5c8486SAndreas Gohr    public function validationProvider()
15*5b5c8486SAndreas Gohr    {
16*5b5c8486SAndreas Gohr        return [
17*5b5c8486SAndreas Gohr            ['ant', true],
18*5b5c8486SAndreas Gohr            ['ant.lion', true],
19*5b5c8486SAndreas Gohr            ['ant.lion.cow', true],
20*5b5c8486SAndreas Gohr            ['ant-lion', true],
21*5b5c8486SAndreas Gohr            ['ant-lion.cow', true],
22*5b5c8486SAndreas Gohr            ['4ant', true],
23*5b5c8486SAndreas Gohr            ['ant4', true],
24*5b5c8486SAndreas Gohr            ['ant44lion', true],
25*5b5c8486SAndreas Gohr            ['44', true],
2678c63d53SAndreas Gohr
27*5b5c8486SAndreas Gohr            ['ant.', false],
28*5b5c8486SAndreas Gohr            ['.ant', false],
29*5b5c8486SAndreas Gohr            ['ant-', false],
30*5b5c8486SAndreas Gohr            ['-ant', false],
31*5b5c8486SAndreas Gohr            ['ant--lion', false],
32*5b5c8486SAndreas Gohr            ['ant..lion', false],
33*5b5c8486SAndreas Gohr            ['ant.-lion', false],
34*5b5c8486SAndreas Gohr            ['ant/lion', false],
35*5b5c8486SAndreas Gohr            ['!ant', false],
36*5b5c8486SAndreas Gohr            ['ant lion', false],
37*5b5c8486SAndreas Gohr        ];
3878c63d53SAndreas Gohr    }
3978c63d53SAndreas Gohr
4078c63d53SAndreas Gohr    /**
4178c63d53SAndreas Gohr     * @dataProvider validationProvider
4278c63d53SAndreas Gohr     * @param $input
4378c63d53SAndreas Gohr     * @param $expect
4478c63d53SAndreas Gohr     */
45*5b5c8486SAndreas Gohr    public function test_validateAnimalName($input, $expect)
46*5b5c8486SAndreas Gohr    {
4778c63d53SAndreas Gohr        /** @var helper_plugin_farmer $helper */
4878c63d53SAndreas Gohr        $helper = plugin_load('helper', 'farmer');
4978c63d53SAndreas Gohr        $this->assertEquals($expect, $helper->validateAnimalName($input));
5078c63d53SAndreas Gohr    }
5178c63d53SAndreas Gohr
52*5b5c8486SAndreas Gohr    public function test_isInPath()
53*5b5c8486SAndreas Gohr    {
5478c63d53SAndreas Gohr        /** @var helper_plugin_farmer $helper */
5578c63d53SAndreas Gohr        $helper = plugin_load('helper', 'farmer');
5678c63d53SAndreas Gohr
5778c63d53SAndreas Gohr        $this->assertTrue($helper->isInPath('/var/www/foo', '/var/www'));
5878c63d53SAndreas Gohr        $this->assertFalse($helper->isInPath('/var/www/../foo', '/var/www'));
5978c63d53SAndreas Gohr
60dfdaf33eSAndreas Gohr        // same dir should return false, too
61dfdaf33eSAndreas Gohr        $this->assertFalse($helper->isInPath('/var/www/foo', '/var/www/foo'));
62dfdaf33eSAndreas Gohr        $this->assertFalse($helper->isInPath('/var/www/foo/', '/var/www/foo'));
63dfdaf33eSAndreas Gohr        $this->assertFalse($helper->isInPath('/var/www/foo/bar/../', '/var/www/foo'));
640f0a264cSAndreas Gohr
650f0a264cSAndreas Gohr        // https://github.com/cosmocode/dokuwiki-plugin-farmer/issues/30
660f0a264cSAndreas Gohr        $this->assertFalse($helper->isInPath('/var/lib/dokuwiki.animals', '/var/lib/dokuwiki'));
6778c63d53SAndreas Gohr    }
6878c63d53SAndreas Gohr}
69