Completed
Push — dev2 ( dd5bec...049fe2 )
by Gordon
39:28 queued 36:22
created

testValidIdentifier()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 30
ccs 0
cts 17
cp 0
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @package comments
5
 */
6
class FindElasticaPageExtensionTest extends ElasticsearchBaseTest {
7
	public static $fixture_file = 'elastica/tests/ElasticaTest.yml';
8
9
10
	/**
11
	 * Test a valid identifier
12
	 */
13
	public function testValidIdentifier() {
14
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
15
16
		//FindElasticaPageExtension is attached to SiteTree so we can search with any page.  Here it
17
		//convenient to use the target pgae in order to test for comparison
18
		$found = $searchPage->getSearchPage('testsearchpage');
19
20
		$this->assertEquals($searchPage->ID, $found->ID);
21
		$this->assertEquals($searchPage->ClassName, $found->ClassName);
22
		$this->assertEquals($searchPage->Title, $found->Title);
23
		$this->assertEquals('testsearchpage', $found->Identifier);
24
25
		// don't check the server name as this will differ, just check the path is /search/
26
		$uri = $searchPage->SearchPageURI('testsearchpage');
27
		$splits = explode('/', $uri);
28
		$this->assertEquals($splits[3],'search');
29
30
		// check the form
31
		$form = $searchPage->SearchPageForm('testsearchpage');
32
		$this->assertInstanceOf('ElasticSearchForm',$form);
33
34
		$fields = $form->Fields();
35
		$actions = $form->Actions();
36
37
		// check for the query text box field
38
		$this->assertEquals('q', $fields->FieldByName('q')->getName());
39
40
		// check for the submit button
41
		$this->assertEquals('action_submit', $actions->FieldByName('action_submit')->getName());
42
	}
43
44
45
	public function testButtonOverride() {
46
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
47
		$buttonText = 'Search Me!';
48
		$form = $searchPage->SearchPageForm('testsearchpage',$buttonText);
49
		$actions = $form->Actions();
50
		$this->assertEquals($buttonText, $actions->FieldByName('action_submit')->Title());
51
	}
52
53
54
	public function testInvalidIdentifier() {
55
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
56
57
		//FindElasticaPageExtension is attached to SiteTree so we can search with any page.  Here it
58
		//convenient to use the target pgae in order to test for comparison
59
		$found = $searchPage->getSearchPage('notasearchpageidentifier');
60
61
		$this->assertEquals(null, $found);
62
		$uri = $searchPage->SearchPageURI('notasearchpageidentifier');
63
64
		$this->assertEquals('', $uri);
65
66
		$this->assertEquals(null, $searchPage->SearchPageForm('notasearchpageidentifier'));
67
	}
68
69
70
	public function testNullIdentifier() {
71
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
72
73
		//FindElasticaPageExtension is attached to SiteTree so we can search with any page.  Here it
74
		//convenient to use the target pgae in order to test for comparison
75
		$found = $searchPage->getSearchPage(null);
76
		$this->assertEquals(null, $found);
77
		$uri = $searchPage->SearchPageURI(null);
78
		$this->assertEquals('', $uri);
79
		$this->assertEquals(null, $searchPage->SearchPageForm(null));
80
	}
81
82
83
	public function testDuplicateIdentifier() {
84
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
85
86
		$esp = new ElasticSearchPage();
87
		// ensure default identifier
88
		$esp->Identifier = $searchPage->Identifier;
89
		$esp->Title='This should not be saved';
90
		try {
91
			$esp->write();
92
			$this->assertFalse(true, 'Duplicate identifier was incorrectly saved');
93
		} catch (Exception $e) {
94
			$this->assertTrue(true,'The page could not be saved as expected, due to duplicate '.
95
								'identifier');
96
		}
97
	}
98
99
}
100