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\Media;
10use dokuwiki\plugin\struct\types\Text;
11
12/**
13 * Tests for the basic validation functions
14 *
15 * @group plugin_struct
16 * @group plugins
17 *
18 */
19class ValidatorTest extends StructTest
20{
21
22    public function setUp(): void
23    {
24        parent::setUp();
25
26        $this->loadSchemaJSON('schema1');
27        $this->loadSchemaJSON('schema2');
28
29        $this->saveData(
30            'page01',
31            'schema1',
32            [
33                'first' => 'first data',
34                'second' => ['second data', 'more data', 'even more'],
35                'third' => 'third data',
36                'fourth' => 'fourth data'
37            ]
38        );
39    }
40
41    protected function tearDown(): void
42    {
43        parent::tearDown();
44
45        /** @var \helper_plugin_struct_db $sqlite */
46        $sqlite = plugin_load('helper', 'struct_db');
47        $sqlite->resetDB();
48        Assignments::reset();
49    }
50
51    public function test_validate_nonArray()
52    {
53        $label = 'label';
54        $errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
55        $integer = new Decimal();
56
57        $validator = new mock\ValueValidator();
58        $value = 'NaN';
59        $this->assertFalse($validator->validateField($integer, $label, $value));
60        $this->assertEquals([$errormsg], $validator->getErrors());
61    }
62
63    public function test_validate_array()
64    {
65        $label = 'label';
66        $errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
67        $integer = new Decimal();
68
69        $validator = new mock\ValueValidator();
70        $value = ['NaN', 'NaN'];
71        $this->assertFalse($validator->validateField($integer, $label, $value));
72        $this->assertEquals([$errormsg, $errormsg], $validator->getErrors());
73    }
74
75    public function test_validate_blank()
76    {
77        $integer = new Decimal();
78
79        $validator = new mock\ValueValidator();
80        $value = null;
81        $this->assertTrue($validator->validateField($integer, 'label', $value));
82        $this->assertEquals([], $validator->getErrors());
83    }
84
85    public function test_validate_clean()
86    {
87        $text = new Text();
88
89        $validator = new mock\ValueValidator();
90        $value = '  foo  ';
91        $this->assertTrue($validator->validateField($text, 'label', $value));
92        $this->assertEquals('foo', $value);
93
94        $value = ['  foo  ', '  bar  '];
95        $this->assertTrue($validator->validateField($text, 'label', $value));
96        $this->assertEquals(['foo', 'bar'], $value);
97    }
98
99    public function test_validate_empty_multivalue()
100    {
101        $lookup = new Lookup(null, '', true);
102        $col = new Column(10, $lookup);
103
104        $validator = new mock\ValueValidator();
105        $value = '';
106
107        $validator->validateValue($col, $value);
108        $this->assertEquals([''], $value);
109
110        // some fields like media or date can post an array with multiple empty strings
111        // because they use multiple inputs instead of comma separation in one input
112        $media = new Media(null, '', true);
113        $col = new Column(10, $media);
114
115        $validator = new mock\ValueValidator();
116        $value = ['', '', ''];
117
118        $validator->validateValue($col, $value);
119        $this->assertEquals([''], $value);
120    }
121
122}
123