|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ElasticSearchPage_Validator_Test extends ElasticsearchFunctionalTestBase { |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
public static $fixture_file = 'elastica/tests/ElasticaTest.yml'; |
|
6
|
|
|
|
|
7
|
|
|
public function setUp() { |
|
8
|
|
|
parent::setUp(); |
|
9
|
|
|
$this->ElasticSearchPage = $this->objFromFixture('ElasticSearchPage', 'search'); |
|
10
|
|
|
|
|
11
|
|
|
$this->ElasticSearchPage->SimilarityStopWords = 'a,the,which'; |
|
12
|
|
|
$this->ElasticSearchPage->write(); |
|
13
|
|
|
|
|
14
|
|
|
$this->Validator = $this->ElasticSearchPage->getCMSValidator(); |
|
|
|
|
|
|
15
|
|
|
$fields = $this->ElasticSearchPage->getCMSFields(); |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
private function getGoodData() { |
|
20
|
|
|
return array( |
|
21
|
|
|
'Title' => 'Name', |
|
22
|
|
|
'URLSegment' => 'name', |
|
23
|
|
|
'MenuTitle' => 'Name', |
|
24
|
|
|
'Identifier' => 'fredfred', |
|
25
|
|
|
'Content' =>' <p>content</p>', |
|
26
|
|
|
'MetaDescription' => null, |
|
27
|
|
|
'ExtraMeta' => null, |
|
28
|
|
|
'ContentForEmptySearch' => '<p>asdfsad</p>', |
|
29
|
|
|
'NewTransLang' => 'de_DE', |
|
30
|
|
|
'AlternativeTemplate' => null, |
|
31
|
|
|
'SiteTreeOnly' => 1, |
|
32
|
|
|
'ClassesToSearch'=> '', |
|
33
|
|
|
'ResultsPerPage' => 10, |
|
34
|
|
|
'AutoCompleteFieldID' => null, |
|
35
|
|
|
'AutoCompleteFunctionID' => null, |
|
36
|
|
|
'SearchHelper' => null, |
|
37
|
|
|
'SimilarityStopWords' => array('a', 'an', 'the',), |
|
38
|
|
|
'MinTermFreq' => 2, |
|
39
|
|
|
'MaxTermFreq' => 25, |
|
40
|
|
|
'MinDocFreq' => 2, |
|
41
|
|
|
'MaxDocFreq' => 0, |
|
42
|
|
|
'MinWordLength' => 0, |
|
43
|
|
|
'MaxWordLength' => 0, |
|
44
|
|
|
'MinShouldMatch' => 0, |
|
45
|
|
|
'Locale' => 'en_US', |
|
46
|
|
|
'ClassName' => 'ElasticSearchPage', |
|
47
|
|
|
'ParentID' => 2738, |
|
48
|
|
|
'ID' => 2739 |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function testGoodData() { |
|
54
|
|
|
$fields = $this->ElasticSearchPage->getCMSFields(); |
|
55
|
|
|
$form = new Form($this, 'Form', $fields, new FieldList()); |
|
|
|
|
|
|
56
|
|
|
$this->ElasticSearchPage->publish('Stage', 'Live'); |
|
57
|
|
|
|
|
58
|
|
|
$fields = $this->ElasticSearchPage->getCMSFields(); |
|
59
|
|
|
$fields->push(new HiddenField('ID')); |
|
60
|
|
|
$form = new Form($this, 'Form', $fields, new FieldList()); |
|
|
|
|
|
|
61
|
|
|
$form->loadDataFrom($this->ElasticSearchPage); |
|
62
|
|
|
$form->setValidator($this->ElasticSearchPage->getCMSValidator()); |
|
63
|
|
|
$this->assertTrue($form->validate(), "Saved object should already be validatable at the form level"); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$origMode = Versioned::get_reading_mode(); |
|
66
|
|
|
Versioned::set_reading_mode('Stage'); |
|
67
|
|
|
Versioned::set_reading_mode($origMode); // reset current mode |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
public function testEmptyIdentifier() { |
|
72
|
|
|
$this->checkForError('Identifier', '', |
|
73
|
|
|
'"Identifier to allow this page to be found in form templates" is required'); |
|
74
|
|
|
$this->checkForError('Identifier', null, |
|
75
|
|
|
'"Identifier to allow this page to be found in form templates" is required'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
public function testEmptyResultsPerPage() { |
|
80
|
|
|
$this->checkForError('ResultsPerPage', '', |
|
81
|
|
|
"'' is not a number, only numbers can be accepted for this field"); |
|
82
|
|
|
$this->checkForError('ResultsPerPage', '', |
|
83
|
|
|
"Results per page must be >=1"); |
|
84
|
|
|
$this->checkForError('ResultsPerPage', 0, |
|
85
|
|
|
"Results per page must be >=1"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
public function testEmptyClassesToSearchWithoutSiteTree() { |
|
90
|
|
|
$this->ElasticSearchPage->SiteTreeOnly = false; |
|
91
|
|
|
$this->checkForError('ClassesToSearch', '', |
|
92
|
|
|
"Please provide at least one class to search, or select 'Site Tree Only'"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
public function testNonSearchableClassesToSearch() { |
|
97
|
|
|
$this->ElasticSearchPage->SiteTreeOnly = false; |
|
98
|
|
|
$this->checkForError('ClassesToSearch', 'Page,Member', |
|
99
|
|
|
"The class Member must have the Searchable extension"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
public function testNonExistentClassesToSearch() { |
|
104
|
|
|
$this->ElasticSearchPage->SiteTreeOnly = false; |
|
105
|
|
|
$this->checkForError('ClassesToSearch', 'Page,THISCLASSDOESNOTEXIST', |
|
106
|
|
|
"The class THISCLASSDOESNOTEXIST does not exist"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
public function testDuplicateIdentifier() { |
|
111
|
|
|
$esp = new ElasticSearchPage(); |
|
112
|
|
|
$esp->Title = "Test with duplicate identifier"; |
|
113
|
|
|
$esp->Identifier = 'THISWILLBECOPIED'; |
|
114
|
|
|
$esp->write(); |
|
115
|
|
|
$this->checkForError('Identifier', 'THISWILLBECOPIED', |
|
116
|
|
|
'The identifier THISWILLBECOPIED already exists'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
private function checkForError($fieldName, $badValue, $message) { |
|
121
|
|
|
$this->ElasticSearchPage->$fieldName = $badValue; |
|
122
|
|
|
|
|
123
|
|
|
$fields = $this->ElasticSearchPage->getCMSFields(); |
|
124
|
|
|
$fields->push(new HiddenField('ID')); |
|
125
|
|
|
$form = new Form($this, 'Form', $fields, new FieldList()); |
|
|
|
|
|
|
126
|
|
|
$form->loadDataFrom($this->ElasticSearchPage); |
|
127
|
|
|
|
|
128
|
|
|
$form->setValidator($this->ElasticSearchPage->getCMSValidator()); |
|
129
|
|
|
$this->assertFalse($form->validate(), "The alternations to the record are expected to fail"); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$errors = Session::get("FormInfo.{$form->FormName()}.errors"); |
|
132
|
|
|
$found = false; |
|
133
|
|
|
|
|
134
|
|
|
foreach ($errors as $error) { |
|
135
|
|
|
if ($error['message'] == $message && $error['fieldName'] == $fieldName) { |
|
136
|
|
|
$found = true; |
|
137
|
|
|
break; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
$this->assertTrue($found, "Message '$message' found"); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.