1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Column;
6use dokuwiki\plugin\struct\meta\Value;
7use dokuwiki\plugin\struct\types\Text;
8
9/**
10 * @group plugin_struct
11 * @group plugins
12 */
13class Value_struct_test extends StructTest {
14
15    /**
16     * @param bool $multi
17     * @return Column
18     */
19    protected function makeColumn($multi) {
20        return new Column(10, new Text(null, '', $multi));
21    }
22
23    /**
24     * Test setting and getting multi values
25     */
26    public function test_multi() {
27        $col = $this->makeColumn(true);
28        $val = new Value($col, array('one', 'two'));
29        $this->assertSame($col, $val->getColumn());
30        $this->assertEquals(array('one', 'two'), $val->getValue());
31
32        $val->setValue(array('one', '', 'two', ''));
33        $this->assertEquals(array('one', 'two'), $val->getValue());
34
35        $val->setValue(array('one', '0', 'two'));
36        $this->assertEquals(array('one', '0', 'two'), $val->getValue());
37
38        $val->setValue(array('', null, false, "   \n"));
39        $this->assertEquals(array(), $val->getValue());
40
41        $val->setValue('');
42        $this->assertEquals(array(), $val->getValue());
43
44        $val->setValue('0');
45        $this->assertEquals(array('0'), $val->getValue());
46
47        $val->setValue(0);
48        $this->assertEquals(array('0'), $val->getValue());
49
50        $val->setValue(array());
51        $this->assertEquals(array(), $val->getValue());
52    }
53
54    /**
55     * Test setting and getting single values
56     */
57    public function test_single() {
58        $col = $this->makeColumn(false);
59        $val = new Value($col, 'one');
60        $this->assertSame($col, $val->getColumn());
61        $this->assertEquals('one', $val->getValue());
62
63        $val->setValue('0');
64        $this->assertEquals('0', $val->getValue());
65
66        $val->setValue('');
67        $this->assertEquals('', $val->getValue());
68
69        $val->setValue("   \n");
70        $this->assertEquals('', $val->getValue());
71
72        $val->setValue(null);
73        $this->assertEquals('', $val->getValue());
74
75        $val->setValue(false);
76        $this->assertEquals('', $val->getValue());
77
78        $val->setValue(array('what', 'the', 'foo'));
79        $this->assertEquals('what', $val->getValue());
80
81        $val->setValue(array());
82        $this->assertEquals('', $val->getValue());
83
84        $val->setValue("  * hi\n  * ho");
85        $this->assertEquals("  * hi\n  * ho", $val->getValue());
86    }
87
88    /**
89     * empty values should not render
90     */
91    public function test_blankrender() {
92        $R = new \Doku_Renderer_xhtml();
93
94        $val = new Value($this->makeColumn(false), '');
95        $val->render($R, 'xhtml');
96        $this->assertEquals('', $R->doc);
97
98        $val = new Value($this->makeColumn(true), array());
99        $val->render($R, 'xhtml');
100        $this->assertEquals('', $R->doc);
101    }
102}
103