1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Test the functionality of the Searchable extension |
5
|
|
|
* @package elastica |
6
|
|
|
*/ |
7
|
|
|
class SearchableFieldTest extends ElasticsearchBaseTest { |
8
|
|
|
public static $fixture_file = 'elastica/tests/ElasticaTest.yml'; |
9
|
|
|
|
10
|
1 |
|
public function testCMSFields() { |
11
|
1 |
|
$sf = new SearchableField(); |
12
|
1 |
|
$sf->Name = 'TestField'; |
|
|
|
|
13
|
1 |
|
$sf->ClazzName = 'TestClazz'; |
|
|
|
|
14
|
1 |
|
$sf->write(); |
15
|
|
|
|
16
|
1 |
|
$fields = $sf->getCMSFields(); |
17
|
|
|
|
18
|
1 |
|
$tab = $this->checkTabExists($fields,'Main'); |
19
|
|
|
|
20
|
|
|
//Check fields |
21
|
1 |
|
$nf = $this->checkFieldExists($tab, 'Name'); |
22
|
1 |
|
$this->assertTrue($nf->isDisabled()); |
23
|
1 |
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/* Zero weight is pointless as it means not part of the search */ |
29
|
2 |
View Code Duplication |
public function testZeroWeight() { |
|
|
|
|
30
|
1 |
|
$searchPage = $this->objFromFixture('ElasticSearchPage', 'search'); |
31
|
1 |
|
$lastEdited = $searchPage->LastEdited; |
32
|
|
|
|
33
|
1 |
|
$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1, |
34
|
1 |
|
'Weight' => 0); |
35
|
1 |
|
$esfs = $searchPage->ElasticaSearchableFields(); |
|
|
|
|
36
|
1 |
|
foreach ($esfs as $sf) { |
37
|
1 |
|
if ($sf->Name == 'Title' || $sf->Name == 'Description') { |
38
|
1 |
|
$esfs->remove($sf); |
39
|
1 |
|
$esfs->add($sf, $extraFields); |
40
|
1 |
|
} |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
try { |
44
|
1 |
|
$searchPage->write(); |
45
|
|
|
$this->fail('Searchable fail should have failed to write'); |
46
|
1 |
|
} catch (ValidationException $e) { |
47
|
1 |
|
$this->assertInstanceOf('ValidationException', $e); |
|
|
|
|
48
|
|
|
$expected = 'The field SearchableTestPage.Title has a zero weight. ; The field '. |
49
|
1 |
|
'SiteTree.Title has a zero weight. ; The field Page.Title has a zero weight. '; |
50
|
1 |
|
$this->assertEquals($expected, $e->getMessage()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
//Effectively assert that the search page has not been written by checking LastEdited |
54
|
1 |
|
$pageFromDB = DataObject::get_by_id('ElasticSearchPage', $searchPage->ID); |
55
|
2 |
|
$this->assertEquals($lastEdited, $pageFromDB->LastEdited); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/* Weights must be positive */ |
60
|
1 |
View Code Duplication |
public function testNegativeWeight() { |
|
|
|
|
61
|
1 |
|
$searchPage = $this->objFromFixture('ElasticSearchPage', 'search'); |
62
|
1 |
|
$lastEdited = $searchPage->LastEdited; |
63
|
|
|
|
64
|
1 |
|
$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1, |
65
|
1 |
|
'Weight' => -1); |
66
|
1 |
|
$esfs = $searchPage->ElasticaSearchableFields(); |
|
|
|
|
67
|
1 |
|
foreach ($esfs as $sf) { |
68
|
1 |
|
if ($sf->Name == 'Title' || $sf->Name == 'Description') { |
69
|
1 |
|
$esfs->remove($sf); |
70
|
1 |
|
$esfs->add($sf, $extraFields); |
71
|
1 |
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
1 |
|
$searchPage->write(); |
76
|
|
|
$this->fail('Searchable fail should have failed to write'); |
77
|
1 |
|
} catch (ValidationException $e) { |
78
|
1 |
|
$this->assertInstanceOf('ValidationException', $e); |
79
|
|
|
$expected = 'The field SearchableTestPage.Title has a negative weight. ; The field '. |
80
|
1 |
|
'SiteTree.Title has a negative weight. ; The field Page.Title has a negative weight. '; |
81
|
1 |
|
$this->assertEquals($expected, $e->getMessage()); |
82
|
|
|
} |
83
|
|
|
//Effectively assert that the search page has not been written by checking LastEdited |
84
|
1 |
|
$pageFromDB = DataObject::get_by_id('ElasticSearchPage', $searchPage->ID); |
85
|
1 |
|
$this->assertEquals($lastEdited, $pageFromDB->LastEdited); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
public function testDefaultWeight() { |
90
|
1 |
|
$searchPage = $this->objFromFixture('ElasticSearchPage', 'search'); |
91
|
|
|
//$searchPage->write(); |
|
|
|
|
92
|
1 |
|
$sf = $searchPage->ElasticaSearchableFields()->first(); |
|
|
|
|
93
|
1 |
|
$this->assertEquals(1, $sf->Weight); |
94
|
1 |
|
$this->assertTrue($sf->ID > 0); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
1 |
|
public function testPositiveWeight() { |
99
|
1 |
|
$sf = new SearchableField(); |
100
|
1 |
|
$sf->Name = 'TestField'; |
|
|
|
|
101
|
1 |
|
$sf->Weight = 10; |
|
|
|
|
102
|
1 |
|
$sf->write(); |
103
|
1 |
|
$this->assertEquals(10, $sf->Weight); |
|
|
|
|
104
|
1 |
|
$this->assertTrue($sf->ID > 0); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
1 |
|
public function testHumanReadableSearchable() { |
109
|
|
|
|
110
|
1 |
|
$searchPage = $this->objFromFixture('ElasticSearchPage', 'search'); |
111
|
|
|
|
112
|
|
|
//ensure valid |
113
|
1 |
|
$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1, |
|
|
|
|
114
|
1 |
|
'Weight' => 1); |
115
|
1 |
|
$sf = $searchPage->ElasticaSearchableFields()->first(); |
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
1 |
|
$sf->Name = 'TestField'; |
119
|
1 |
|
$sf->Searchable = false; |
120
|
1 |
|
$this->assertEquals('No', $sf->HumanReadableSearchable()); |
121
|
1 |
|
$sf->Searchable = true; |
122
|
1 |
|
$this->assertEquals('Yes', $sf->HumanReadableSearchable()); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// ---- searchable fields are created via a script, so test do not allow creation/deletion ---- |
127
|
|
|
|
128
|
|
|
/* |
129
|
|
|
Ensure CMS users cannot delete searchable fields |
130
|
|
|
*/ |
131
|
1 |
|
public function testCanDelete() { |
132
|
1 |
|
$sf = new SearchableField(); |
133
|
1 |
|
$this->assertFalse($sf->canDelete()); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/* |
137
|
|
|
Ensure CMS users cannot create searchable fields |
138
|
|
|
*/ |
139
|
1 |
|
public function testCanCreate() { |
140
|
1 |
|
$sf = new SearchableField(); |
141
|
1 |
|
$this->assertFalse($sf->canCreate()); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.