Completed
Push — dev2 ( 774f9d...0919fb )
by Gordon
05:50
created

testEmptyClassesToSearchWithoutSiteTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
class ElasticSearchPage_Validator_Test extends ElasticsearchFunctionalTestBase {
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The property Validator does not seem to exist. Did you mean originalMemberPasswordValidator?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
15
		$fields = $this->ElasticSearchPage->getCMSFields();
0 ignored issues
show
Unused Code introduced by
$fields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$this is of type this<ElasticSearchPage_Validator_Test>, but the function expects a object<Controller>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$this is of type this<ElasticSearchPage_Validator_Test>, but the function expects a object<Controller>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ElasticSearchPage_Validator_Test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
$this is of type this<ElasticSearchPage_Validator_Test>, but the function expects a object<Controller>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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");
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<ElasticSearchPage_Validator_Test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<ElasticSearchPage_Validator_Test>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
	}
142
143
144
}
145