|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\Constraint\ViewableDataContains; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\Dev\Tests\ViewableDataContainsTest\TestObject; |
|
8
|
|
|
use SilverStripe\Security\Member; |
|
9
|
|
|
use SilverStripe\View\ArrayData; |
|
10
|
|
|
|
|
11
|
|
|
class ViewableDataContainsTest extends SapphireTest |
|
12
|
|
|
{ |
|
13
|
|
|
private $test_data = [ |
|
14
|
|
|
'FirstName' => 'Ingo', |
|
15
|
|
|
'Surname' => 'Schommer' |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
public function provideMatchesForList() |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
|
|
[ |
|
22
|
|
|
['FirstName' => 'Ingo'] |
|
23
|
|
|
], |
|
24
|
|
|
[ |
|
25
|
|
|
['Surname' => 'Schommer'] |
|
26
|
|
|
], |
|
27
|
|
|
[ |
|
28
|
|
|
['FirstName' => 'Ingo', 'Surname' => 'Schommer'] |
|
29
|
|
|
] |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function provideInvalidMatchesForList() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
[ |
|
38
|
|
|
['FirstName' => 'AnyoneNotInList'] |
|
39
|
|
|
], |
|
40
|
|
|
[ |
|
41
|
|
|
['Surname' => 'NotInList'] |
|
42
|
|
|
], |
|
43
|
|
|
[ |
|
44
|
|
|
['FirstName' => 'Ingo', 'Surname' => 'Minnee'] |
|
45
|
|
|
] |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @dataProvider provideMatchesForList() |
|
51
|
|
|
* |
|
52
|
|
|
* @param $match |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testEvaluateMatchesCorrectlyArrayData($match) |
|
55
|
|
|
{ |
|
56
|
|
|
$constraint = new ViewableDataContains($match); |
|
57
|
|
|
|
|
58
|
|
|
$item = ArrayData::create($this->test_data); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertTrue($constraint->evaluate($item, '', true)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @dataProvider provideMatchesForList() |
|
65
|
|
|
* |
|
66
|
|
|
* @param $match |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testEvaluateMatchesCorrectlyDataObject($match) |
|
69
|
|
|
{ |
|
70
|
|
|
$constraint = new ViewableDataContains($match); |
|
71
|
|
|
|
|
72
|
|
|
$item = Member::create($this->test_data); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertTrue($constraint->evaluate($item, '', true)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @dataProvider provideInvalidMatchesForList() |
|
79
|
|
|
* |
|
80
|
|
|
* @param $matches |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testEvaluateDoesNotMatchWrongMatchInArrayData($match) |
|
83
|
|
|
{ |
|
84
|
|
|
$constraint = new ViewableDataContains($match); |
|
85
|
|
|
|
|
86
|
|
|
$item = ArrayData::create($this->test_data); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertFalse($constraint->evaluate($item, '', true)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @dataProvider provideInvalidMatchesForList() |
|
93
|
|
|
* |
|
94
|
|
|
* @param $matches |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testEvaluateDoesNotMatchWrongMatchInDataObject($match) |
|
97
|
|
|
{ |
|
98
|
|
|
$constraint = new ViewableDataContains($match); |
|
99
|
|
|
|
|
100
|
|
|
$item = Member::create($this->test_data); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertFalse($constraint->evaluate($item, '', true)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testFieldAccess() |
|
106
|
|
|
{ |
|
107
|
|
|
$data = new TestObject(['name' => 'Damian']); |
|
108
|
|
|
$constraint = new ViewableDataContains(['name' => 'Damian', 'Something' => 'something']); |
|
109
|
|
|
$this->assertTrue($constraint->evaluate($data, '', true)); |
|
110
|
|
|
|
|
111
|
|
|
$constraint = new ViewableDataContains(['name' => 'Damian', 'Something' => 'notthing']); |
|
112
|
|
|
$this->assertFalse($constraint->evaluate($data, '', true)); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|