1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Column;
6use dokuwiki\plugin\struct\test\mock\Assignments;
7use dokuwiki\plugin\struct\test\mock\Lookup;
8use dokuwiki\plugin\struct\types\Decimal;
9use dokuwiki\plugin\struct\types\Text;
10
11/**
12 * Tests for the basic validation functions
13 *
14 * @group plugin_struct
15 * @group plugins
16 *
17 */
18class Validator_struct_test extends StructTest
19{
20
21    public function setUp(): void
22    {
23        parent::setUp();
24
25        $this->loadSchemaJSON('schema1');
26        $this->loadSchemaJSON('schema2');
27
28        $this->saveData(
29            'page01',
30            'schema1',
31            array(
32                'first' => 'first data',
33                'second' => array('second data', 'more data', 'even more'),
34                'third' => 'third data',
35                'fourth' => 'fourth data'
36            )
37        );
38    }
39
40    protected function tearDown(): void
41    {
42        parent::tearDown();
43
44        /** @var \helper_plugin_struct_db $sqlite */
45        $sqlite = plugin_load('helper', 'struct_db');
46        $sqlite->resetDB();
47        Assignments::reset();
48    }
49
50    public function test_validate_nonArray()
51    {
52        $label = 'label';
53        $errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
54        $integer = new Decimal();
55
56        $validator = new mock\ValueValidator();
57        $value = 'NaN';
58        $this->assertFalse($validator->validateField($integer, $label, $value));
59        $this->assertEquals(array($errormsg), $validator->getErrors());
60    }
61
62    public function test_validate_array()
63    {
64        $label = 'label';
65        $errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
66        $integer = new Decimal();
67
68        $validator = new mock\ValueValidator();
69        $value = array('NaN', 'NaN');
70        $this->assertFalse($validator->validateField($integer, $label, $value));
71        $this->assertEquals(array($errormsg, $errormsg), $validator->getErrors());
72    }
73
74    public function test_validate_blank()
75    {
76        $integer = new Decimal();
77
78        $validator = new mock\ValueValidator();
79        $value = null;
80        $this->assertTrue($validator->validateField($integer, 'label', $value));
81        $this->assertEquals(array(), $validator->getErrors());
82    }
83
84    public function test_validate_clean()
85    {
86        $text = new Text();
87
88        $validator = new mock\ValueValidator();
89        $value = '  foo  ';
90        $this->assertTrue($validator->validateField($text, 'label', $value));
91        $this->assertEquals('foo', $value);
92
93        $value = array('  foo  ', '  bar  ');
94        $this->assertTrue($validator->validateField($text, 'label', $value));
95        $this->assertEquals(array('foo', 'bar'), $value);
96    }
97
98    public function test_validate_empty_multivalue()
99    {
100        $lookup = new Lookup(null, '', true);
101        $col = new Column(10, $lookup);
102
103        $validator = new mock\ValueValidator();
104        $value = '';
105
106        $validator->validateValue($col, $value);
107        $this->assertEquals([''], $value);
108    }
109
110}
111