Completed
Push — dev2 ( 76a09a...1cd385 )
by Gordon
14:51
created

SearchableTest::testFieldsToElasticaConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 91
Code Lines 65

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 91
rs 8.5181
cc 1
eloc 65
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 SearchableTest extends ElasticsearchBaseTest {
11
	public static $fixture_file = 'elastica/tests/ElasticaTest.yml';
12
13 View Code Duplication
	public function setUp() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
		// this needs to be called in order to create the list of searchable
15
		// classes and fields that are available.  Simulates part of a build
16
		$classes = array('SearchableTestPage', 'SiteTree', 'Page', 'FlickrPhotoTO', 'FlickrSetTO',
17
			'FlickrTagTO', 'FlickrAuthorTO', 'FlickrSetTO');
18
		$this->requireDefaultRecordsFrom = $classes;
19
20
		// load fixtures
21
		parent::setUp();
22
	}
23
24
25
26
	public function testgetFieldValuesAsArrayFromFixtures() {
27
		$manyTypes = $this->objFromFixture('ManyTypesPage', 'manytypes0001');
28
		$result = $manyTypes->getFieldValuesAsArray();
29
		$this->generateAssertionsFromArray($result);
30
		$expected = array(
31
			'BooleanField' => '1',
32
			'CurrencyField' => '100.25',
33
			'DateField' => '2014-04-15',
34
			'DecimalField' => '0',
35
			'EnumField' => '',
36
			'HTMLTextField' => '',
37
			'HTMLVarcharField' => 'This is some *HTML*varchar field',
38
			'IntField' => '677',
39
			'PercentageField' => '27',
40
			'SS_DatetimeField' => '2014-10-18 08:24:00',
41
			'TextField' => 'This is a text field',
42
			'TimeField' => '17:48:18',
43
			'Title' => 'Many Types Page',
44
			'Content' => 'Many types of fields',
45
		);
46
		$this->assertEquals($expected, $result);
47
48
	}
49
50
51
52
	public function testBadFormatFields() {
53
		$manyTypes = $this->objFromFixture('ManyTypesPage', 'manytypes0001');
54
		$fields = $manyTypes->getElasticaFields();
55
56
		$expected = array('type' => 'boolean');
57
		$this->assertEquals($expected, $fields['BooleanField']);
58
59
		$expected = array('type' => 'double');
60
		$this->assertEquals($expected, $fields['CurrencyField']);
61
62
		$expected = array('type' => 'date', 'format' => 'y-M-d');
63
		$this->assertEquals($expected, $fields['DateField']);
64
65
		$expected = array('type' => 'double');
66
		$this->assertEquals($expected, $fields['DecimalField']);
67
68
		$stringFormat = array(
69
			'type' => 'string',
70
			'analyzer' => 'stemmed',
71
			'term_vector' => 'yes',
72
			'fields' => array(
73
				'standard' => array(
74
					'type' => 'string',
75
					'analyzer' => 'unstemmed',
76
					'term_vector' => 'yes'
77
				),
78
				'shingles' => array(
79
					'type' => 'string',
80
					'analyzer' => 'shingles',
81
					'term_vector' => 'yes'
82
				)
83
			)
84
		);
85
		$expected = $stringFormat;
86
		$this->assertEquals($expected, $fields['EnumField']);
87
88
		$expected = $stringFormat;
89
		$this->assertEquals($expected, $fields['HTMLTextField']);
90
91
		$expected = $stringFormat;
92
		$this->assertEquals($expected, $fields['HTMLVarcharField']);
93
94
		$expected = array('type' => 'integer');
95
		$this->assertEquals($expected, $fields['IntField']);
96
97
		$expected = array('type' => 'double');
98
		$this->assertEquals($expected, $fields['PercentageField']);
99
100
		$expected = array('type' => 'date', 'format' => 'y-M-d H:m:s');
101
		$this->assertEquals($expected, $fields['SS_DatetimeField']);
102
103
		$expected = $stringFormat;
104
		$this->assertEquals($expected, $fields['TextField']);
105
106
		$expected = array('type' => 'date', 'format' => 'H:m:s');
107
		$this->assertEquals($expected, $fields['TimeField']);
108
	}
109
110
111
	public function testGetDateFields() {
112
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
113
		$fields = $flickrPhoto->getElasticaFields();
114
115
		$expected = array('type' => 'date', 'format' => 'y-M-d H:m:s');
116
		$this->assertEquals($expected, $fields['TakenAt']);
117
118
		$expected = array('type' => 'date', 'format' => 'y-M-d H:m:s');
119
		$this->assertEquals($expected, $fields['TakenAtDT']);
120
121
		$expected = array('type' => 'date', 'format' => 'y-M-d');
122
		$this->assertEquals($expected, $fields['FirstViewed']);
123
	}
124
125
126
	/**
127
	 * Test a valid identifier
128
	 */
129
	public function testMapping() {
130
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
131
		$mapping = $flickrPhoto->getElasticaMapping();
132
133
		//array of mapping properties
134
		$properties = $mapping->getProperties();
135
136
		//test FlickrPhotoTO relationships mapping
137
		$expectedRelStringArray = array(
138
			'type' => 'string',
139
			'fields' => array(
140
				'standard' => array(
141
					'type' => 'string',
142
					'analyzer' => 'unstemmed',
143
					'term_vector' => 'yes'
144
				),
145
				'shingles' => array(
146
					'type' => 'string',
147
					'analyzer' => 'shingles',
148
					'term_vector' => 'yes'
149
				)
150
			),
151
			'analyzer' => 'stemmed',
152
			'term_vector' => 'yes'
153
		);
154
155
		$this->assertEquals($expectedRelStringArray,
156
			$properties['FlickrAuthorTO']['properties']['DisplayName']
157
		);
158
		$this->assertEquals($expectedRelStringArray,
159
			$properties['FlickrAuthorTO']['properties']['PathAlias']
160
		);
161
		$this->assertEquals($expectedRelStringArray,
162
			$properties['FlickrTagTO']['properties']['RawValue']
163
		);
164
		$this->assertEquals($expectedRelStringArray,
165
			$properties['FlickrSetTO']['properties']['Title']
166
		);
167
		$this->assertEquals($expectedRelStringArray,
168
			$properties['FlickrSetTO']['properties']['Description']
169
		);
170
171
		// check constructed field, location
172
		$locationProperties = $properties['location'];
173
		$this->assertEquals('geo_point', $locationProperties['type']);
174
		$this->assertEquals('compressed', $locationProperties['fielddata']['format']);
175
		$this->assertEquals('1cm', $locationProperties['fielddata']['precision']);
176
177
178
		//test the FlickrPhotoTO core model
179
180
181
182
		// check strings
183
		$shouldBeString = array('Title', 'Description');
184
		$shouldBeInt = array('ISO', 'FlickrID', 'FocalLength35mm');
185
		$shouldBeBoolean = array('IsInSiteTree');
186
		$shouldBeDouble = array('Aperture');
187
		$shouldBeDateTime = array('TakenAt');
188
		$shouldBeDate = array('FirstViewed');
189
190
		// tokens are strings that have analyzer 'not_analyzed', namely the string is indexed as is
191
		$shouldBeTokens = array('ShutterSpeed', 'Link');
192
193
194
		// check strings
195
		$expectedStandardArray = array('type' => 'string', 'analyzer' => 'unstemmed', 'term_vector' => 'yes');
196
		foreach($shouldBeString as $fieldName) {
197
			$fieldProperties = $properties[$fieldName];
198
199
			$type = $fieldProperties['type'];
200
			$analyzer = $fieldProperties['analyzer'];
201
			$this->assertEquals('string', $type);
202
203
			// check for stemmed analysis
204
			$this->assertEquals('stemmed', $analyzer);
205
206
			// check for unstemmed analaysis
207
208
			$this->assertEquals($expectedStandardArray, $fieldProperties['fields']['standard']);
209
210
			// check for only 3 entries
211
			$this->assertEquals(4, sizeof(array_keys($fieldProperties)));
212
		}
213
214
		// check ints
215
		foreach($shouldBeInt as $fieldName) {
216
			$fieldProperties = $properties[$fieldName];
217
			$type = $fieldProperties['type'];
218
			$this->assertEquals(1, sizeof(array_keys($fieldProperties)));
219
			$this->assertEquals('integer', $type);
220
		}
221
222
223
		// check doubles
224
		foreach($shouldBeDouble as $fieldName) {
225
			$fieldProperties = $properties[$fieldName];
226
			$type = $fieldProperties['type'];
227
			$this->assertEquals(1, sizeof(array_keys($fieldProperties)));
228
			$this->assertEquals('double', $type);
229
		}
230
231
		// check boolean
232
		foreach($shouldBeBoolean as $fieldName) {
233
			$fieldProperties = $properties[$fieldName];
234
			$type = $fieldProperties['type'];
235
			$this->assertEquals(1, sizeof(array_keys($fieldProperties)));
236
			$this->assertEquals('boolean', $type);
237
		}
238
239
240
		foreach($shouldBeDate as $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
			$fieldProperties = $properties[$fieldName];
242
			$type = $fieldProperties['type'];
243
			$this->assertEquals(2, sizeof(array_keys($fieldProperties)));
244
			$this->assertEquals('date', $type);
245
			$this->assertEquals('y-M-d', $fieldProperties['format']);
246
		}
247
248
249
250
		// check date time, stored in Elasticsearch as a date with a different format than above
251
		foreach($shouldBeDateTime as $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252
			$fieldProperties = $properties[$fieldName];
253
			$type = $fieldProperties['type'];
254
			$this->assertEquals(2, sizeof(array_keys($fieldProperties)));
255
			$this->assertEquals('date', $type);
256
			$this->assertEquals('y-M-d H:m:s', $fieldProperties['format']);
257
		}
258
259
		//check shutter speed is tokenized, ie not analyzed - for aggregation purposes
260
		//
261
		foreach($shouldBeTokens as $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
			$fieldProperties = $properties[$fieldName];
263
			$type = $fieldProperties['type'];
264
			$this->assertEquals('string', $type);
265
266
			// check for no analysis
267
			$analyzer = $fieldProperties['index'];
268
			$this->assertEquals('not_analyzed', $analyzer);
269
270
			// check for only 2 entries
271
			$this->assertEquals(2, sizeof(array_keys($fieldProperties)));
272
		}
273
	}
274
275
276
	public function testGetType() {
277
		//A type in Elasticsearch is used to represent each SilverStripe content type,
278
		//the name used being the Silverstripe $fieldName
279
280
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
281
		$type = $flickrPhoto->getElasticaType();
282
		$this->assertEquals('FlickrPhotoTO', $type);
283
	}
284
285
286
	/*
287
	Get a record as an Elastic document and check values
288
	 */
289
	public function testGetElasticaDocument() {
290
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
291
		$doc = $flickrPhoto->getElasticaDocument()->getData();
292
293
		$expected = array();
294
		$expected['Title'] = 'Bangkok';
295
		$expected['FlickrID'] = '1234567';
296
		$expected['Description'] = 'Test photograph';
297
		$expected['TakenAt'] = '2011-07-04 20:36:00';
298
		$expected['TakenAtDT'] = null;
299
		$expected['FirstViewed'] = '2012-04-28';
300
		$expected['Aperture'] = 8.0;
301
302
		//Shutter speed is altered for aggregations
303
		$expected['ShutterSpeed'] = '0.01|1/100';
304
		$expected['FocalLength35mm'] = 140;
305
		$expected['ISO'] = 400;
306
		$expected['AspectRatio'] = 1.013;
307
		$expected['Photographer'] = array();
308
		$expected['FlickrTagTOs'] = array();
309
		$expected['FlickrSetTOs'] = array();
310
		$expected['IsInSiteTree'] = false;
311
		$expected['location'] = array('lat' => 13.42, 'lon' => 100);
312
		$expected['TestMethod'] = 'this is a test method';
313
		$expected['TestMethodHTML'] = 'this is a test method that returns *HTML*';
314
		$this->assertEquals($expected, $doc);
315
	}
316
317
318
	public function testElasticaResult() {
319
		$resultList = $this->getResultsFor('Bangkok');
320
321
		// there is only one result.  Note lack of a 'first' method
322
		foreach($resultList->getIterator() as $fp) {
323
			//This is an Elastica\Result object
324
			$elasticaResult = $fp->getElasticaResult();
325
326
			$fields = $elasticaResult->getSource();
327
328
			$this->assertEquals($fp->Title, $fields['Title']);
329
			$this->assertEquals($fp->FlickrID, $fields['FlickrID']);
330
			$this->assertEquals($fp->Description, $fields['Description']);
331
			$this->assertEquals($fp->TakenAt, $fields['TakenAt']);
332
			$this->assertEquals($fp->FirstViewed, $fields['FirstViewed']);
333
			$this->assertEquals($fp->Aperture, $fields['Aperture']);
334
335
			//ShutterSpeed is a special case, mangled field
336
			$this->assertEquals('0.01|1/100', $fields['ShutterSpeed']);
337
			$this->assertEquals($fp->FocalLength35mm, $fields['FocalLength35mm']);
338
			$this->assertEquals($fp->ISO, $fields['ISO']);
339
			$this->assertEquals($fp->AspectRatio, $fields['AspectRatio']);
340
341
			//Empty arrays for null values
342
			$this->assertEquals(array(), $fields['Photographer']);
343
			$this->assertEquals(array(), $fields['FlickrTagTOs']);
344
			$this->assertEquals(array(), $fields['FlickrSetTOs']);
345
			$this->assertEquals(false, $fields['IsInSiteTree']);
346
		}
347
	}
348
349
350
	public function testDeleteNonExistentDoc() {
351
		$fp = new FlickrPhotoTO();
352
		$fp->Title = 'Test Deletion';
353
		$fp->IndexingOff = true; // do no index this
354
		$fp->write();
355
		$fp->IndexingOff = false;
356
357
		try {
358
			$fp->delete();
359
			$this->fail('Exception should have been thrown when deleting non existent item');
360
		} catch (Exception $e) {
361
			//This error comes out of Elastica itself
362
			$this->assertEquals('Deleted document FlickrPhotoTO (2) not found in search index.',
363
				$e->getMessage());
364
		}
365
	}
366
367
368
369
370
	public function testUnpublishPublish() {
371
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
372
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
373
374
		$page = $this->objFromFixture('SiteTree', 'sitetree001');
375
		$page->doUnpublish();
376
377
		$this->checkNumberOfIndexedDocuments($nDocsAtStart - 1);
378
379
		$page->doPublish();
380
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
381
	}
382
383
384
	/**
385
	 * For a page that is already published, set the ShowInSearch flag to false,
386
	 * write to stage, and then rePublish
387
	 */
388
	public function testUnpublishAlreadyPublisedhHideFromSearch() {
389
		$page = $this->objFromFixture('SiteTree', 'sitetree001');
390
391
		// By default the page is not indexed (for speed reasons)
392
		// Change the title, turn on indexing and save it
393
		// This will invoke a database write
394
		$page->Title = "I will be indexed";
395
		$page->IndexingOff = true;
396
		$page->write();
397
398
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
399
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
400
401
		// assert keys of term vectors, this will indicate page
402
		// is stored in the index or not
403
		$termVectors = $page->getTermVectors();
404
		$expected = array(
405
		'0' => 'Content',
406
		'1' => 'Content.shingles',
407
		'2' => 'Content.standard',
408
		'3' => 'Link',
409
				'4' => 'Title',
410
		'5' => 'Title.autocomplete',
411
		'6' => 'Title.shingles',
412
		'7' => 'Title.standard',
413
		);
414
415
		$keys = array_keys($termVectors);
416
		sort($keys);
417
418
		$this->assertEquals($expected, $keys);
419
420
421
//CURRENT
422
		$page->ShowInSearch = false;
423
		$page->write();
424
425
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
426
427
		$page->doPublish();
428
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
429
	}
430
431
432
433
	/**
434
	 * For a page that is not published, set the ShowInSearch flag to false,
435
	 * write to stage, and then rePublish.  Same as previous test except
436
	 * no need to delete from the index as it already does not exist
437
	 */
438
	public function testUnpublishPublishHideFromSearch() {
439
		$page = $this->objFromFixture('SiteTree', 'sitetree001');
440
		$page->doUnpublish();
441
442
		// By default the page is not indexed (for speed reasons)
443
		// Change the title, turn on indexing and save it
444
		// This will invoke a database write
445
		$page->Title = "I will be indexed";
446
		$page->IndexingOff = true;
447
		$page->write();
448
449
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
450
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
451
		$page->ShowInSearch = false;
452
		$page->write();
453
454
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
455
456
		$page->doPublish();
457
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
458
	}
459
460
461
462
463
	public function testGetCMSFields() {
464
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
465
		$fields = $flickrPhoto->getCMSFields();
466
467
		$this->checkTabExists($fields, 'ElasticaTermsset');
468
	}
469
470
471
	public function testNoSearchableFieldsConfigured() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
472
		$config = Config::inst();
473
		$sf = $config->get('FlickrPhotoTO', 'searchable_fields');
474
		$config->remove('FlickrPhotoTO', 'searchable_fields');
475
		$fp = Injector::inst()->create('FlickrPhotoTO');
476
		try {
477
			$fp->getAllSearchableFields();
478
			$this->fail("getAllSearchableFields should have failed as static var searchable_fields not configured");
479
		} catch (Exception $e) {
480
			$this->assertEquals('The field $searchable_fields must be set for the class FlickrPhotoTO', $e->getMessage());
481
		}
482
483
		$config->update('FlickrPhotoTO', 'searchable_fields', $sf);
484
	}
485
486
487
	public function testNoSearchableFieldsConfiguredForHasManyRelation() {
488
		$config = Config::inst();
489
		$sf = $config->get('FlickrTagTO', 'searchable_fields');
490
		$config->remove('FlickrTagTO', 'searchable_fields');
491
		$fp = Injector::inst()->create('FlickrPhotoTO');
492
		try {
493
			$fp->getAllSearchableFields();
494
			$this->fail("getAllSearchableFields should have failed as static var searchable_fields not configured");
495
		} catch (Exception $e) {
496
			$this->assertEquals('The field $searchable_fields must be set for the class FlickrTagTO', $e->getMessage());
497
		}
498
499
		$config->update('FlickrTagTO', 'searchable_fields', $sf);
500
501
	}
502
503
504
	public function testNoSearchableFieldsConfiguredForHasOneRelation() {
505
		$config = Config::inst();
506
		$sf = $config->get('FlickrAuthorTO', 'searchable_fields');
507
		$config->remove('FlickrAuthorTO', 'searchable_fields');
508
		$fp = Injector::inst()->create('FlickrPhotoTO');
509
		try {
510
			$fp->getAllSearchableFields();
511
			$this->fail("getAllSearchableFields should have failed as static var searchable_fields not configured");
512
		} catch (Exception $e) {
513
			$this->assertEquals('The field $searchable_fields must be set for the class FlickrAuthorTO', $e->getMessage());
514
		}
515
516
		$config->update('FlickrAuthorTO', 'searchable_fields', $sf);
517
518
	}
519
520
521
	public function testSearchableMethodNotExist() {
522
		$config = Config::inst();
523
		$sr = $config->get('FlickrPhotoTO', 'searchable_relationships');
524
		$config->remove('FlickrPhotoTO', 'searchable_relationships');
525
		$config->update('FlickrPhotoTO', 'searchable_relationships', array('thisMethodDoesNotExist'));
526
		$fp = Injector::inst()->create('FlickrPhotoTO');
527
		try {
528
			$fp->getAllSearchableFields();
529
			$this->fail("getAllSearchableFields should have failed searchable relationship does not exist");
530
		} catch (Exception $e) {
531
			$this->assertEquals('The method thisMethodDoesNotExist not found in class FlickrPhotoTO, please check configuration',
532
				 $e->getMessage());
533
		}
534
535
		// MUST REMOVE FIRST.  Otherwise append and the erroroneus value above still exists
536
		$config->remove('FlickrPhotoTO', 'searchable_relationships');
537
		$config->update('FlickrPhotoTO', 'searchable_relationships', $sr);
538
	}
539
540
541
	public function testFieldsToElasticaConfig() {
542
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
543
		$fields = $flickrPhoto->getAllSearchableFields();
544
545
		$expected = array(
546
			'Title' => array(
547
				'title' => 'Title',
548
				'filter' => 'PartialMatchFilter'
549
			),
550
			'FlickrID' => array(
551
				'title' => 'Flickr ID',
552
				'filter' => 'PartialMatchFilter'
553
			),
554
			'Description' => array(
555
				'title' => 'Description',
556
				'filter' => 'PartialMatchFilter'
557
			),
558
			'TakenAt' => array(
559
				'title' => 'Taken At',
560
				'filter' => 'PartialMatchFilter'
561
			),
562
			'TakenAtDT' => array(
563
				'title' => 'Taken At DT',
564
				'filter' => 'PartialMatchFilter'
565
			),
566
			'FirstViewed' => array(
567
				'title' => 'First Viewed',
568
				'filter' => 'PartialMatchFilter'
569
			),
570
			'Aperture' => array(
571
				'title' => 'Aperture',
572
				'filter' => 'PartialMatchFilter'
573
			),
574
			'ShutterSpeed' => array(
575
				'title' => 'Shutter Speed',
576
				'filter' => 'PartialMatchFilter'
577
			),
578
			'FocalLength35mm' => array(
579
				'title' => 'Focal Length35mm',
580
				'filter' => 'PartialMatchFilter'
581
			),
582
			'ISO' => array(
583
				'title' => 'ISO',
584
				'filter' => 'PartialMatchFilter'
585
			),
586
			'AspectRatio' => array(
587
				'title' => 'Aspect Ratio',
588
				'filter' => 'PartialMatchFilter'
589
			),
590
			'TestMethod' => array(
591
				'title' => 'Test Method',
592
				'filter' => 'PartialMatchFilter'
593
			),
594
			'TestMethodHTML' => array(
595
				'title' => 'Test Method HTML',
596
				'filter' => 'PartialMatchFilter'
597
			),
598
			'Photographer()' => array(
599
				'PathAlias' => array(
600
					'title' => 'Path Alias',
601
					'filter' => 'PartialMatchFilter'
602
				),
603
				'DisplayName' => array(
604
					'title' => 'Display Name',
605
					'filter' => 'PartialMatchFilter'
606
				)
607
			),
608
			'FlickrTagTOs()' => array(
609
				'RawValue' => array(
610
					'title' => 'Raw Value',
611
					'filter' => 'PartialMatchFilter'
612
				)
613
			),
614
			'FlickrSetTOs()' => array(
615
				'Title' => array(
616
					'title' => 'Title',
617
					'filter' => 'PartialMatchFilter'
618
				),
619
				'FlickrID' => array(
620
					'title' => 'Flickr ID',
621
					'filter' => 'PartialMatchFilter'
622
				),
623
				'Description' => array(
624
					'title' => 'Description',
625
					'filter' => 'PartialMatchFilter'
626
				)
627
			)
628
		);
629
630
		$this->assertEquals($expected, $fields);
631
	}
632
633
634
	public function testHasOneExistsSearchableToArray() {
635
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
636
		$flickrPhoto->IndexingOff = false;
637
		$flickrPhoto->Title = 'Test title edited';
638
		$photographer = new FlickrAuthorTO();
639
		$photographer->DisplayName = 'Fred Bloggs';
640
		$photographer->PathAlias = '/fredbloggs';
641
642
		$photographer->write();
643
644
		$flickrPhoto->PhotographerID = $photographer->ID; ;
645
		$flickrPhoto->write();
646
		$fieldValuesArray = $flickrPhoto->getFieldValuesAsArray();
647
648
		$actual = $fieldValuesArray['Photographer'];
649
		$this->generateAssertionsFromArray($actual);
650
		$expected = array(
651
			'PathAlias' => '/fredbloggs',
652
			'DisplayName' => 'Fred Bloggs',
653
			'FlickrPhotoTO' => '',
654
		);
655
656
		$this->assertEquals($expected, $actual);
657
	}
658
659
660
661
	public function testHasManyExistsSearchableToArray() {
662
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
663
		$flickrPhoto->IndexingOff = false;
664
		$flickrPhoto->Title = 'Test title edited';
665
		$tag1 = new FlickrTagTO();
666
		$tag1->FlickrID = '1000001';
667
		$tag1->Value = 'auckland';
668
		$tag1->RawValue = 'Auckland';
669
		$tag1->write();
670
671
672
		$tag2 = new FlickrTagTO();
673
		$tag2->FlickrID = '1000002';
674
		$tag2->Value = 'wellington';
675
		$tag2->RawValue = 'Wellington';
676
		$tag2->write();
677
678
		$flickrPhoto->FlickrTagTOs()->add($tag1);
679
		$flickrPhoto->FlickrTagTOs()->add($tag2);
680
		$flickrPhoto->write();
681
		$fieldValuesArray = $flickrPhoto->getFieldValuesAsArray();
682
		$actual = $fieldValuesArray['Photographer'];
683
		$this->assertEquals(array(), $actual);
684
685
		$actual = $fieldValuesArray['FlickrTagTOs'];
686
		$this->generateAssertionsFromArrayRecurse($actual);
687
688
		$expected = array(
689
			'0' => array(
690
				'RawValue' => 'Auckland'
691
			),
692
			'1' => array(
693
				'RawValue' => 'Wellington'
694
			)
695
		);
696
697
698
		$this->assertEquals($expected, $actual);
699
	}
700
701
702
	public function testUpdateCMSFieldsDatabject() {
703
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
704
		$flickrPhoto->IndexingOff = false;
705
		$flickrPhoto->Title = 'Test title edited';
706
		$flickrPhoto->write();
707
		$fields = $flickrPhoto->getCMSFields();
708
709
		$tabset = $fields->findOrMakeTab('Root.ElasticaTerms');
710
		$tabNames = array();
711 View Code Duplication
		foreach($tabset->Tabs() as $tab) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
712
			$tabFields = array();
713
			foreach($tab->FieldList() as $field) {
714
				array_push($tabFields, $field->getName());
715
			}
716
			$expectedName = 'TermsFor' . $tab->getName(); ;
717
			$expected = array($expectedName);
718
			$this->assertEquals($expected, $tabFields);
719
			array_push($tabNames, $tab->getName());
720
		}
721
		$expected = array('Description', 'Description_shingles', 'Description_standard',
722
			'ShutterSpeed', 'TestMethod', 'TestMethod_shingles', 'TestMethod_standard',
723
			'TestMethodHTML', 'TestMethodHTML_shingles', 'TestMethodHTML_standard',
724
			'Title', 'Title_autocomplete', 'Title_shingles', 'Title_standard');
725
726
		$this->assertEquals($expected, $tabNames);
727
	}
728
729
730
	public function testUpdateCMSFieldsSiteTreeLive() {
731
		$page = $this->objFromFixture('SearchableTestPage', 'first');
732
		$page->IndexingOff = false;
733
		$page->Title = 'Test title edited';
734
		$page->write();
735
		$page->doPublish();
736
		$fields = $page->getCMSFields();
737
738
		$tabset = $fields->findOrMakeTab('Root.ElasticaTerms');
739
		$tabNames = array();
740 View Code Duplication
		foreach($tabset->Tabs() as $tab) {
741
			$tabFields = array();
742
			foreach($tab->FieldList() as $field) {
743
				array_push($tabFields, $field->getName());
744
			}
745
			$expectedName = 'TermsFor' . $tab->getName(); ;
746
			$expected = array($expectedName);
747
			$this->assertEquals($expected, $tabFields);
748
			array_push($tabNames, $tab->getName());
749
		}
750
		$expected = array(
751
			'Content', 'Content_standard', 'Link', 'Title', 'Title_autocomplete', 'Title_shingles',
752
			'Title_standard');
753
		$this->generateAssertionsFromArray1D($tabNames);
754
		$this->assertEquals($expected, $tabNames);
755
756
	}
757
758
759 View Code Duplication
	private function getResultsFor($query, $pageLength = 10) {
760
		$es = new ElasticSearcher();
761
		$es->setStart(0);
762
		$es->setPageLength($pageLength);
763
		$es->setClasses('FlickrPhotoTO');
764
		$fields = array('Title' => 1, 'Description' => 1);
765
		$resultList = $es->search($query, $fields)->getList();
766
		$this->assertEquals('SilverStripe\Elastica\ResultList', get_class($resultList));
767
		return $resultList;
768
	}
769
770
}
771