1<?php
2
3/**
4 * Tests for the validation functionality of the farmer plugin
5 *
6 * @group plugin_farmer
7 * @group plugins
8 */
9class helper_plugin_farmer_test extends DokuWikiTest
10{
11
12    protected $pluginsEnabled = ['farmer',];
13
14    public function validationProvider()
15    {
16        return [
17            ['ant', true],
18            ['ant.lion', true],
19            ['ant.lion.cow', true],
20            ['ant-lion', true],
21            ['ant-lion.cow', true],
22            ['4ant', true],
23            ['ant4', true],
24            ['ant44lion', true],
25            ['44', true],
26
27            ['ant.', false],
28            ['.ant', false],
29            ['ant-', false],
30            ['-ant', false],
31            ['ant--lion', false],
32            ['ant..lion', false],
33            ['ant.-lion', false],
34            ['ant/lion', false],
35            ['!ant', false],
36            ['ant lion', false],
37        ];
38    }
39
40    /**
41     * @dataProvider validationProvider
42     * @param $input
43     * @param $expect
44     */
45    public function test_validateAnimalName($input, $expect)
46    {
47        /** @var helper_plugin_farmer $helper */
48        $helper = plugin_load('helper', 'farmer');
49        $this->assertEquals($expect, $helper->validateAnimalName($input));
50    }
51
52    public function test_isInPath()
53    {
54        /** @var helper_plugin_farmer $helper */
55        $helper = plugin_load('helper', 'farmer');
56
57        $this->assertTrue($helper->isInPath('/var/www/foo', '/var/www'));
58        $this->assertFalse($helper->isInPath('/var/www/../foo', '/var/www'));
59
60        // same dir should return false, too
61        $this->assertFalse($helper->isInPath('/var/www/foo', '/var/www/foo'));
62        $this->assertFalse($helper->isInPath('/var/www/foo/', '/var/www/foo'));
63        $this->assertFalse($helper->isInPath('/var/www/foo/bar/../', '/var/www/foo'));
64
65        // https://github.com/cosmocode/dokuwiki-plugin-farmer/issues/30
66        $this->assertFalse($helper->isInPath('/var/lib/dokuwiki.animals', '/var/lib/dokuwiki'));
67    }
68}
69