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    public function setUp() : void {
21        parent::setUp();
22
23        $this->loadSchemaJSON('schema1');
24        $this->loadSchemaJSON('schema2');
25
26        $this->saveData(
27            'page01',
28            'schema1',
29            array(
30                'first' => 'first data',
31                'second' => array('second data', 'more data', 'even more'),
32                'third' => 'third data',
33                'fourth' => 'fourth data'
34            )
35        );
36    }
37
38    protected function tearDown() : void {
39        parent::tearDown();
40
41        /** @var \helper_plugin_struct_db $sqlite */
42        $sqlite = plugin_load('helper', 'struct_db');
43        $sqlite->resetDB();
44        Assignments::reset();
45    }
46
47    public function test_validate_nonArray() {
48        $label = 'label';
49        $errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
50        $integer = new Decimal();
51
52        $validator = new mock\ValueValidator();
53        $value = 'NaN';
54        $this->assertFalse($validator->validateField($integer, $label, $value));
55        $this->assertEquals(array($errormsg), $validator->getErrors());
56    }
57
58    public function test_validate_array() {
59        $label = 'label';
60        $errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
61        $integer = new Decimal();
62
63        $validator = new mock\ValueValidator();
64        $value = array('NaN', 'NaN');
65        $this->assertFalse($validator->validateField($integer, $label, $value));
66        $this->assertEquals(array($errormsg, $errormsg), $validator->getErrors());
67    }
68
69    public function test_validate_blank() {
70        $integer = new Decimal();
71
72        $validator = new mock\ValueValidator();
73        $value = null;
74        $this->assertTrue($validator->validateField($integer, 'label', $value));
75        $this->assertEquals(array(), $validator->getErrors());
76    }
77
78    public function test_validate_clean() {
79        $text = new Text();
80
81        $validator = new mock\ValueValidator();
82        $value = '  foo  ';
83        $this->assertTrue($validator->validateField($text, 'label', $value));
84        $this->assertEquals('foo', $value);
85
86        $value = array('  foo  ', '  bar  ');
87        $this->assertTrue($validator->validateField($text, 'label', $value));
88        $this->assertEquals(array('foo', 'bar'), $value);
89    }
90
91    public function test_validate_empty_multivalue() {
92        $lookup = new Lookup(null, '', true);
93        $col = new Column(10, $lookup);
94
95        $validator = new mock\ValueValidator();
96        $value = '';
97
98        $validator->validateValue($col, $value);
99        $this->assertEquals([''], $value);
100    }
101
102}
103