summaryrefslogtreecommitdiff
path: root/tests/units/Model/ConfigTest.php
blob: 0c108fd13e3042be4f9a75905e0edbf47d0a0e28 (plain)
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
<?php

require_once __DIR__.'/../Base.php';

use Kanboard\Model\Config;
use Kanboard\Core\Session\SessionManager;

class ConfigTest extends Base
{
    public function testCRUDOperations()
    {
        $c = new Config($this->container);

        $this->assertTrue($c->save(array('key1' => 'value1')));
        $this->assertTrue($c->save(array('key1' => 'value2')));
        $this->assertTrue($c->save(array('key2' => 'value2')));

        $this->assertEquals('value2', $c->getOption('key1'));
        $this->assertEquals('value2', $c->getOption('key2'));
        $this->assertEquals('', $c->getOption('key3'));
        $this->assertEquals('default', $c->getOption('key3', 'default'));

        $this->assertTrue($c->exists('key1'));
        $this->assertFalse($c->exists('key3'));

        $this->assertTrue($c->save(array('key1' => 'value1')));

        $this->assertArrayHasKey('key1', $c->getAll());
        $this->assertArrayHasKey('key2', $c->getAll());

        $this->assertContains('value1', $c->getAll());
        $this->assertContains('value2', $c->getAll());
    }

    public function testSaveApplicationUrl()
    {
        $c = new Config($this->container);

        $this->assertTrue($c->save(array('application_url' => 'http://localhost/')));
        $this->assertEquals('http://localhost/', $c->get('application_url'));

        $this->assertTrue($c->save(array('application_url' => 'http://localhost')));
        $this->assertEquals('http://localhost/', $c->get('application_url'));

        $this->assertTrue($c->save(array('application_url' => '')));
        $this->assertEquals('', $c->get('application_url'));
    }

    public function testDefaultValues()
    {
        $c = new Config($this->container);

        $this->assertEquals('en_US', $c->get('application_language'));
        $this->assertEquals('UTC', $c->get('application_timezone'));

        $this->assertEmpty($c->get('webhook_url_task_modification'));
        $this->assertEmpty($c->get('webhook_url_task_creation'));
        $this->assertEmpty($c->get('board_columns'));
        $this->assertEmpty($c->get('application_url'));

        $this->assertNotEmpty($c->get('webhook_token'));
        $this->assertNotEmpty($c->get('api_token'));
    }

    public function testGet()
    {
        $c = new Config($this->container);

        $this->assertEquals('', $c->get('board_columns'));
        $this->assertEquals('test', $c->get('board_columns', 'test'));
        $this->assertEquals(0, $c->get('board_columns', 0));
    }

    public function testGetWithSession()
    {
        $c = new Config($this->container);

        session_id('test');
        $this->assertTrue(SessionManager::isOpen());

        $this->assertEquals('', $c->get('board_columns'));
        $this->assertEquals('test', $c->get('board_columns', 'test'));

        $this->container['sessionStorage']->config = array(
            'board_columns' => 'foo',
            'empty_value' => 0
        );

        $this->assertEquals('foo', $c->get('board_columns'));
        $this->assertEquals('foo', $c->get('board_columns', 'test'));
        $this->assertEquals('test', $c->get('empty_value', 'test'));

        session_id('');
        $this->assertFalse(SessionManager::isOpen());
    }
}