testMappingWithLocale()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
crap 6
1
<?php
2
3
use SilverStripe\Elastica\ElasticSearcher;
4
5
6
/**
7
 * Test the functionality of the Searchable extension
8
 * @package elastica
9
 */
10
class TranslatableSearchableTest extends ElasticsearchBaseTest {
11
	public static $fixture_file = 'elastica/tests/ElasticaTest.yml';
12
13
14
15
	public function setUpOnce() {
16
		//Add translatable if it exists
17
		if (class_exists('Translatable')) {
18
			SiteTree::add_extension('Translatable');
19
		}
20
		parent::setUpOnce();
21
	}
22
23
24
	public function setUp() {
25
		// this needs to be called in order to create the list of searchable
26
		// classes and fields that are available.  Simulates part of a build
27
		$classes = array('SearchableTestPage', 'SiteTree', 'Page', 'FlickrPhotoTO', 'FlickrSetTO',
28
			'FlickrTagTO', 'FlickrAuthorTO', 'FlickrSetTO');
29
		$this->requireDefaultRecordsFrom = $classes;
30
		// load fixtures
31
		parent::setUp();
32
	}
33
34
35
36
	public function testgetFieldValuesAsArrayWithLocale() {
37
		if(!class_exists('Translatable')) {
38
			$this->markTestSkipped('Translatable not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<TranslatableSearchableTest>.

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...
39
		}
40
41
		$manyTypes = $this->objFromFixture('ManyTypesPage', 'manytypes0001');
42
		$result = $manyTypes->getFieldValuesAsArray();
43
		$this->generateAssertionsFromArray($result);
44
45
		$expected = array(
46
			'BooleanField' => '1',
47
			'CurrencyField' => '100.25',
48
			'DateField' => '2014-04-15',
49
			'DecimalField' => '0',
50
			'EnumField' => '',
51
			'HTMLTextField' => '',
52
			'HTMLVarcharField' => 'This is some *HTML*varchar field',
53
			'IntField' => '677',
54
			'PercentageField' => '27',
55
			'SS_DatetimeField' => '2014-10-18 08:24:00',
56
			'TextField' => 'This is a text field',
57
			'TimeField' => '17:48:18',
58
			'Title' => 'Many Types Page',
59
			'Content' => 'Many types of fields',
60
		);
61
		$this->assertEquals($expected, $result);
62
63
	}
64
65
66
	/**
67
	 * Test a valid identifier
68
	 */
69
	public function testMappingWithLocale() {
70
		if(!class_exists('Translatable')) {
71
			$this->markTestSkipped('Translatable not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<TranslatableSearchableTest>.

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...
72
		}
73
74
		$manyTypes = $this->objFromFixture('ManyTypesPage', 'manytypes0001');
75
		$mapping = $manyTypes->getElasticaMapping();
76
77
		// Only check mapping of locale as that is what has changed
78
		$properties = $mapping->getProperties();
79
		$localeProperties = $properties['Locale'];
80
		$this->assertEquals(array('type' => 'string', 'index' => 'not_analyzed'), $localeProperties);
81
	}
82
83
84
85
	/*
86
	Get a record as an Elastic document and check values
87
	 */
88
	public function testGetElasticaDocumentWithLocale() {
89
		if(!class_exists('Translatable')) {
90
			$this->markTestSkipped('Translatable not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<TranslatableSearchableTest>.

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...
91
		}
92
93
		$this->markTestSkipped('Translatable not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<TranslatableSearchableTest>.

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...
94
95
		// Need to get something from the SiteTree
96
		$manyTypes = $this->objFromFixture('ManyTypesPage', 'manytypes0001');
97
		$result = $manyTypes->getFieldValuesAsArray();
0 ignored issues
show
Unused Code introduced by
$result 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...
98
		$doc = $manyTypes->getElasticaDocument()->getData();
99
100
		$expected = array(
101
			'BooleanField' => '1',
102
			'CurrencyField' => '100.25',
103
			'DateField' => '2014-04-15',
104
			'DecimalField' => '0',
105
			'EnumField' => '',
106
			'HTMLTextField' => '',
107
			'HTMLVarcharField' => 'This is some *HTML*varchar field',
108
			'IntField' => '677',
109
			'PercentageField' => '27',
110
			'SS_DatetimeField' => '2014-10-18 08:24:00',
111
			'TextField' => 'This is a text field',
112
			'TimeField' => '17:48:18',
113
			'Title' => 'Many Types Page',
114
			'Content' => 'Many types of fields',
115
			'IsInSiteTree' => '1',
116
			'Link' => 'http://moduletest.silverstripe/many-types-page/',
117
			'Locale' => 'en_US',
118
		);
119
		$this->assertEquals($expected, $doc);
120
	}
121
122
}
123