aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/config/ConfigPluginTest.php
blob: 3a45f623da7a8ff1b1750c8582084437034fae2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Shaarli\Config;

use Shaarli\Config\Exception\PluginConfigOrderException;
use Shaarli\Plugin\PluginManager;

require_once 'application/config/ConfigPlugin.php';

/**
 * Unitary tests for Shaarli config related functions
 */
class ConfigPluginTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Test save_plugin_config with valid data.
     *
     * @throws PluginConfigOrderException
     */
    public function testSavePluginConfigValid()
    {
        $data = [
            'order_plugin1' => 2,   // no plugin related
            'plugin2' => 0,         // new - at the end
            'plugin3' => 0,         // 2nd
            'order_plugin3' => 8,
            'plugin4' => 0,         // 1st
            'order_plugin4' => 5,
        ];

        $expected = [
            'plugin3',
            'plugin4',
            'plugin2',
        ];

        mkdir($path = __DIR__ . '/folder');
        PluginManager::$PLUGINS_PATH = $path;
        array_map(function (string $plugin) use ($path) { touch($path . '/' . $plugin); }, $expected);

        $out = save_plugin_config($data);
        $this->assertEquals($expected, $out);

        array_map(function (string $plugin) use ($path) { unlink($path . '/' . $plugin); }, $expected);
        rmdir($path);
    }

    /**
     * Test save_plugin_config with invalid data.
     */
    public function testSavePluginConfigInvalid()
    {
        $this->expectException(\Shaarli\Config\Exception\PluginConfigOrderException::class);

        $data = array(
            'plugin2' => 0,
            'plugin3' => 0,
            'order_plugin3' => 0,
            'plugin4' => 0,
            'order_plugin4' => 0,
        );

        save_plugin_config($data);
    }

    /**
     * Test save_plugin_config without data.
     */
    public function testSavePluginConfigEmpty()
    {
        $this->assertEquals(array(), save_plugin_config(array()));
    }

    /**
     * Test validate_plugin_order with valid data.
     */
    public function testValidatePluginOrderValid()
    {
        $data = array(
            'order_plugin1' => 2,
            'plugin2' => 0,
            'plugin3' => 0,
            'order_plugin3' => 1,
            'plugin4' => 0,
            'order_plugin4' => 5,
        );

        $this->assertTrue(validate_plugin_order($data));
    }

    /**
     * Test validate_plugin_order with invalid data.
     */
    public function testValidatePluginOrderInvalid()
    {
        $data = array(
            'order_plugin1' => 2,
            'order_plugin3' => 1,
            'order_plugin4' => 1,
        );

        $this->assertFalse(validate_plugin_order($data));
    }

    /**
     * Test load_plugin_parameter_values.
     */
    public function testLoadPluginParameterValues()
    {
        $plugins = array(
            'plugin_name' => array(
                'parameters' => array(
                    'param1' => array('value' => true),
                    'param2' => array('value' => false),
                    'param3' => array('value' => ''),
                )
            )
        );

        $parameters = array(
            'param1' => 'value1',
            'param2' => 'value2',
        );

        $result = load_plugin_parameter_values($plugins, $parameters);
        $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']['value']);
        $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']['value']);
        $this->assertEquals('', $result['plugin_name']['parameters']['param3']['value']);
    }
}