Completed
Push — dev2 ( 26ca88...f79151 )
by Gordon
03:18
created

testHasManyExistsSearchableToArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 45
rs 8.8571
cc 1
eloc 29
nc 1
nop 0
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
	public function setUp() {
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
		print_r($fields);
116
		$expected = array('type' => 'date', 'format' => 'y-M-d H:m:s');
117
		$this->assertEquals($expected, $fields['TakenAt']);
118
119
		$expected = array('type' => 'date', 'format' => 'y-M-d H:m:s');
120
		$this->assertEquals($expected, $fields['TakenAtDT']);
121
122
		$expected = array('type' => 'date', 'format' => 'y-M-d');
123
		$this->assertEquals($expected, $fields['FirstViewed']);
124
	}
125
126
127
	/**
128
	 * Test a valid identifier
129
	 */
130
	public function testMapping() {
131
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
132
		$mapping = $flickrPhoto->getElasticaMapping();
133
134
		//array of mapping properties
135
		$properties = $mapping->getProperties();
136
137
		//test FlickrPhotoTO relationships mapping
138
		$expectedRelStringArray = array(
139
			'type' => 'string',
140
			'fields' => array(
141
				'standard' => array(
142
					'type' => 'string',
143
					'analyzer' => 'unstemmed',
144
					'term_vector' => 'yes'
145
				),
146
				'shingles' => array(
147
					'type' => 'string',
148
					'analyzer' => 'shingles',
149
					'term_vector' => 'yes'
150
				)
151
			),
152
			'analyzer' => 'stemmed',
153
			'term_vector' => 'yes'
154
		);
155
156
		$this->assertEquals($expectedRelStringArray,
157
			$properties['FlickrAuthorTO']['properties']['DisplayName']
158
		);
159
		$this->assertEquals($expectedRelStringArray,
160
			$properties['FlickrAuthorTO']['properties']['PathAlias']
161
		);
162
		$this->assertEquals($expectedRelStringArray,
163
			$properties['FlickrTagTO']['properties']['RawValue']
164
		);
165
		$this->assertEquals($expectedRelStringArray,
166
			$properties['FlickrSetTO']['properties']['Title']
167
		);
168
		$this->assertEquals($expectedRelStringArray,
169
			$properties['FlickrSetTO']['properties']['Description']
170
		);
171
172
		// check constructed field, location
173
		$locationProperties = $properties['location'];
174
		$this->assertEquals('geo_point', $locationProperties['type']);
175
		$this->assertEquals('compressed', $locationProperties['fielddata']['format']);
176
		$this->assertEquals('1cm', $locationProperties['fielddata']['precision']);
177
178
179
		//test the FlickrPhotoTO core model
180
181
182
183
		// check strings
184
		$shouldBeString = array('Title', 'Description');
185
		$shouldBeInt = array('ISO', 'FlickrID', 'FocalLength35mm');
186
		$shouldBeBoolean = array('IsInSiteTree');
187
		$shouldBeDouble = array('Aperture');
188
		$shouldBeDateTime = array('TakenAt');
189
		$shouldBeDate = array('FirstViewed');
190
191
		// tokens are strings that have analyzer 'not_analyzed', namely the string is indexed as is
192
		$shouldBeTokens = array('ShutterSpeed', 'Link');
193
194
195
		// check strings
196
		$expectedStandardArray = array('type' => 'string', 'analyzer' => 'unstemmed', 'term_vector' => 'yes');
197
		foreach($shouldBeString as $fieldName) {
198
			$fieldProperties = $properties[$fieldName];
199
200
			$type = $fieldProperties['type'];
201
			$analyzer = $fieldProperties['analyzer'];
202
			$this->assertEquals('string', $type);
203
204
			// check for stemmed analysis
205
			$this->assertEquals('stemmed', $analyzer);
206
207
			// check for unstemmed analaysis
208
209
			$this->assertEquals($expectedStandardArray, $fieldProperties['fields']['standard']);
210
211
			// check for only 3 entries
212
			$this->assertEquals(4, sizeof(array_keys($fieldProperties)));
213
		}
214
215
		// check ints
216
		foreach($shouldBeInt as $fieldName) {
217
			$fieldProperties = $properties[$fieldName];
218
			$type = $fieldProperties['type'];
219
			$this->assertEquals(1, sizeof(array_keys($fieldProperties)));
220
			$this->assertEquals('integer', $type);
221
		}
222
223
224
		// check doubles
225
		foreach($shouldBeDouble as $fieldName) {
226
			$fieldProperties = $properties[$fieldName];
227
			$type = $fieldProperties['type'];
228
			$this->assertEquals(1, sizeof(array_keys($fieldProperties)));
229
			$this->assertEquals('double', $type);
230
		}
231
232
		// check boolean
233
		foreach($shouldBeBoolean as $fieldName) {
234
			$fieldProperties = $properties[$fieldName];
235
			$type = $fieldProperties['type'];
236
			$this->assertEquals(1, sizeof(array_keys($fieldProperties)));
237
			$this->assertEquals('boolean', $type);
238
		}
239
240
241
		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...
242
			$fieldProperties = $properties[$fieldName];
243
			$type = $fieldProperties['type'];
244
			$this->assertEquals(2, sizeof(array_keys($fieldProperties)));
245
			$this->assertEquals('date', $type);
246
			$this->assertEquals('y-M-d', $fieldProperties['format']);
247
		}
248
249
250
251
		// check date time, stored in Elasticsearch as a date with a different format than above
252
		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...
253
			$fieldProperties = $properties[$fieldName];
254
			$type = $fieldProperties['type'];
255
			$this->assertEquals(2, sizeof(array_keys($fieldProperties)));
256
			$this->assertEquals('date', $type);
257
			$this->assertEquals('y-M-d H:m:s', $fieldProperties['format']);
258
		}
259
260
		//check shutter speed is tokenized, ie not analyzed - for aggregation purposes
261
		//
262
		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...
263
			$fieldProperties = $properties[$fieldName];
264
			$type = $fieldProperties['type'];
265
			$this->assertEquals('string', $type);
266
267
			// check for no analysis
268
			$analyzer = $fieldProperties['index'];
269
			$this->assertEquals('not_analyzed', $analyzer);
270
271
			// check for only 2 entries
272
			$this->assertEquals(2, sizeof(array_keys($fieldProperties)));
273
		}
274
	}
275
276
277
	public function testGetType() {
278
		//A type in Elasticsearch is used to represent each SilverStripe content type,
279
		//the name used being the Silverstripe $fieldName
280
281
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
282
		$type = $flickrPhoto->getElasticaType();
283
		$this->assertEquals('FlickrPhotoTO', $type);
284
	}
285
286
287
	/*
288
	Get a record as an Elastic document and check values
289
	 */
290
	public function testGetElasticaDocument() {
291
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
292
		$doc = $flickrPhoto->getElasticaDocument()->getData();
293
294
		$expected = array();
295
		$expected['Title'] = 'Bangkok';
296
		$expected['FlickrID'] = '1234567';
297
		$expected['Description'] = 'Test photograph';
298
		$expected['TakenAt'] = '2011-07-04 20:36:00';
299
		$expected['TakenAtDT'] = null;
300
		$expected['FirstViewed'] = '2012-04-28';
301
		$expected['Aperture'] = 8.0;
302
303
		//Shutter speed is altered for aggregations
304
		$expected['ShutterSpeed'] = '0.01|1/100';
305
		$expected['FocalLength35mm'] = 140;
306
		$expected['ISO'] = 400;
307
		$expected['AspectRatio'] = 1.013;
308
		$expected['Photographer'] = array();
309
		$expected['FlickrTagTOs'] = array();
310
		$expected['FlickrSetTOs'] = array();
311
		$expected['IsInSiteTree'] = false;
312
		$expected['location'] = array('lat' => 13.42, 'lon' => 100);
313
		$expected['TestMethod'] = 'this is a test method';
314
		$expected['TestMethodHTML'] = 'this is a test method that returns *HTML*';
315
316
317
		print_r($doc);
318
319
		$this->assertEquals($expected, $doc);
320
	}
321
322
323
	public function testElasticaResult() {
324
		$resultList = $this->getResultsFor('Bangkok');
325
326
		// there is only one result.  Note lack of a 'first' method
327
		foreach($resultList->getIterator() as $fp) {
328
			//This is an Elastica\Result object
329
			$elasticaResult = $fp->getElasticaResult();
330
331
			$fields = $elasticaResult->getSource();
332
333
			$this->assertEquals($fp->Title, $fields['Title']);
334
			$this->assertEquals($fp->FlickrID, $fields['FlickrID']);
335
			$this->assertEquals($fp->Description, $fields['Description']);
336
			$this->assertEquals($fp->TakenAt, $fields['TakenAt']);
337
			$this->assertEquals($fp->FirstViewed, $fields['FirstViewed']);
338
			$this->assertEquals($fp->Aperture, $fields['Aperture']);
339
340
			//ShutterSpeed is a special case, mangled field
341
			$this->assertEquals('0.01|1/100', $fields['ShutterSpeed']);
342
			$this->assertEquals($fp->FocalLength35mm, $fields['FocalLength35mm']);
343
			$this->assertEquals($fp->ISO, $fields['ISO']);
344
			$this->assertEquals($fp->AspectRatio, $fields['AspectRatio']);
345
346
			//Empty arrays for null values
347
			$this->assertEquals(array(), $fields['Photographer']);
348
			$this->assertEquals(array(), $fields['FlickrTagTOs']);
349
			$this->assertEquals(array(), $fields['FlickrSetTOs']);
350
			$this->assertEquals(false, $fields['IsInSiteTree']);
351
		}
352
	}
353
354
355
	public function testDeleteNonExistentDoc() {
356
		$fp = new FlickrPhotoTO();
357
		$fp->Title = 'Test Deletion';
358
		$fp->IndexingOff = true; // do no index this
359
		$fp->write();
360
		$fp->IndexingOff = false;
361
362
		try {
363
			$fp->delete();
364
			$this->fail('Exception should have been thrown when deleting non existent item');
365
		} catch (Exception $e) {
366
			//This error comes out of Elastica itself
367
			$this->assertEquals('Deleted document FlickrPhotoTO (2) not found in search index.',
368
				$e->getMessage());
369
		}
370
	}
371
372
373
374
375
	public function testUnpublishPublish() {
376
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
377
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
378
379
		$page = $this->objFromFixture('SiteTree', 'sitetree001');
380
		$page->doUnpublish();
381
382
		$this->checkNumberOfIndexedDocuments($nDocsAtStart - 1);
383
384
		$page->doPublish();
385
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
386
	}
387
388
389
	/**
390
	 * For a page that is already published, set the ShowInSearch flag to false,
391
	 * write to stage, and then rePublish
392
	 */
393
	public function testUnpublishAlreadyPublisedhHideFromSearch() {
394
		$page = $this->objFromFixture('SiteTree', 'sitetree001');
395
396
		// By default the page is not indexed (for speed reasons)
397
		// Change the title, turn on indexing and save it
398
		// This will invoke a database write
399
		$page->Title = "I will be indexed";
400
		$page->IndexingOff = true;
401
		$page->write();
402
403
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
404
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
405
406
		// assert keys of term vectors, this will indicate page
407
		// is stored in the index or not
408
		$termVectors = $page->getTermVectors();
409
		$expected = array(
410
		'0' => 'Content',
411
		'1' => 'Content.shingles',
412
		'2' => 'Content.standard',
413
		'3' => 'Link',
414
				'4' => 'Title',
415
		'5' => 'Title.autocomplete',
416
		'6' => 'Title.shingles',
417
		'7' => 'Title.standard',
418
		);
419
420
		$keys = array_keys($termVectors);
421
		sort($keys);
422
423
		$this->assertEquals($expected, $keys);
424
425
426
//CURRENT
427
		$page->ShowInSearch = false;
428
		$page->write();
429
430
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
431
432
		$page->doPublish();
433
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
434
	}
435
436
437
438
	/**
439
	 * For a page that is not published, set the ShowInSearch flag to false,
440
	 * write to stage, and then rePublish.  Same as previous test except
441
	 * no need to delete from the index as it already does not exist
442
	 */
443
	public function testUnpublishPublishHideFromSearch() {
444
		$page = $this->objFromFixture('SiteTree', 'sitetree001');
445
		$page->doUnpublish();
446
447
		// By default the page is not indexed (for speed reasons)
448
		// Change the title, turn on indexing and save it
449
		// This will invoke a database write
450
		$page->Title = "I will be indexed";
451
		$page->IndexingOff = true;
452
		$page->write();
453
454
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
455
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
456
		$page->ShowInSearch = false;
457
		$page->write();
458
459
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
460
461
		$page->doPublish();
462
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
463
	}
464
465
466
467
468
	public function testGetCMSFields() {
469
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
470
		$fields = $flickrPhoto->getCMSFields();
471
472
		$this->checkTabExists($fields, 'ElasticaTermsset');
473
	}
474
475
476
	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...
477
		$config = Config::inst();
478
		$sf = $config->get('FlickrPhotoTO', 'searchable_fields');
479
		$config->remove('FlickrPhotoTO', 'searchable_fields');
480
		$fp = Injector::inst()->create('FlickrPhotoTO');
481
		try {
482
			$fp->getAllSearchableFields();
483
			$this->fail("getAllSearchableFields should have failed as static var searchable_fields not configured");
484
		} catch (Exception $e) {
485
			$this->assertEquals('The field $searchable_fields must be set for the class FlickrPhotoTO', $e->getMessage());
486
		}
487
488
		$config->update('FlickrPhotoTO', 'searchable_fields', $sf);
489
	}
490
491
492
	public function testNoSearchableFieldsConfiguredForHasManyRelation() {
493
		$config = Config::inst();
494
		$sf = $config->get('FlickrTagTO', 'searchable_fields');
495
		$config->remove('FlickrTagTO', 'searchable_fields');
496
		$fp = Injector::inst()->create('FlickrPhotoTO');
497
		try {
498
			$fp->getAllSearchableFields();
499
			$this->fail("getAllSearchableFields should have failed as static var searchable_fields not configured");
500
		} catch (Exception $e) {
501
			$this->assertEquals('The field $searchable_fields must be set for the class FlickrTagTO', $e->getMessage());
502
		}
503
504
		$config->update('FlickrTagTO', 'searchable_fields', $sf);
505
506
	}
507
508
509
	public function testNoSearchableFieldsConfiguredForHasOneRelation() {
510
		$config = Config::inst();
511
		$sf = $config->get('FlickrAuthorTO', 'searchable_fields');
512
		$config->remove('FlickrAuthorTO', 'searchable_fields');
513
		$fp = Injector::inst()->create('FlickrPhotoTO');
514
		try {
515
			$fp->getAllSearchableFields();
516
			$this->fail("getAllSearchableFields should have failed as static var searchable_fields not configured");
517
		} catch (Exception $e) {
518
			$this->assertEquals('The field $searchable_fields must be set for the class FlickrAuthorTO', $e->getMessage());
519
		}
520
521
		$config->update('FlickrAuthorTO', 'searchable_fields', $sf);
522
523
	}
524
525
526
	public function testSearchableMethodNotExist() {
527
		$config = Config::inst();
528
		$sr = $config->get('FlickrPhotoTO', 'searchable_relationships');
529
		$config->remove('FlickrPhotoTO', 'searchable_relationships');
530
		$config->update('FlickrPhotoTO', 'searchable_relationships', array('thisMethodDoesNotExist'));
531
		$fp = Injector::inst()->create('FlickrPhotoTO');
532
		try {
533
			$fp->getAllSearchableFields();
534
			$this->fail("getAllSearchableFields should have failed searchable relationship does not exist");
535
		} catch (Exception $e) {
536
			$this->assertEquals('The method thisMethodDoesNotExist not found in class FlickrPhotoTO, please check configuration',
537
				 $e->getMessage());
538
		}
539
540
		// MUST REMOVE FIRST.  Otherwise append and the erroroneus value above still exists
541
		$config->remove('FlickrPhotoTO', 'searchable_relationships');
542
		$config->update('FlickrPhotoTO', 'searchable_relationships', $sr);
543
	}
544
545
546
	public function testFieldsToElasticaConfig() {
547
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
548
		$fields = $flickrPhoto->getAllSearchableFields();
549
550
		$expected = array(
551
			'Title' => array(
552
				'title' => 'Title',
553
				'filter' => 'PartialMatchFilter'
554
			),
555
			'FlickrID' => array(
556
				'title' => 'Flickr ID',
557
				'filter' => 'PartialMatchFilter'
558
			),
559
			'Description' => array(
560
				'title' => 'Description',
561
				'filter' => 'PartialMatchFilter'
562
			),
563
			'TakenAt' => array(
564
				'title' => 'Taken At',
565
				'filter' => 'PartialMatchFilter'
566
			),
567
			'TakenAtDT' => array(
568
				'title' => 'Taken At DT',
569
				'filter' => 'PartialMatchFilter'
570
			),
571
			'FirstViewed' => array(
572
				'title' => 'First Viewed',
573
				'filter' => 'PartialMatchFilter'
574
			),
575
			'Aperture' => array(
576
				'title' => 'Aperture',
577
				'filter' => 'PartialMatchFilter'
578
			),
579
			'ShutterSpeed' => array(
580
				'title' => 'Shutter Speed',
581
				'filter' => 'PartialMatchFilter'
582
			),
583
			'FocalLength35mm' => array(
584
				'title' => 'Focal Length35mm',
585
				'filter' => 'PartialMatchFilter'
586
			),
587
			'ISO' => array(
588
				'title' => 'ISO',
589
				'filter' => 'PartialMatchFilter'
590
			),
591
			'AspectRatio' => array(
592
				'title' => 'Aspect Ratio',
593
				'filter' => 'PartialMatchFilter'
594
			),
595
			'TestMethod' => array(
596
				'title' => 'Test Method',
597
				'filter' => 'PartialMatchFilter'
598
			),
599
			'TestMethodHTML' => array(
600
				'title' => 'Test Method HTML',
601
				'filter' => 'PartialMatchFilter'
602
			),
603
			'Photographer()' => array(
604
				'PathAlias' => array(
605
					'title' => 'Path Alias',
606
					'filter' => 'PartialMatchFilter'
607
				),
608
				'DisplayName' => array(
609
					'title' => 'Display Name',
610
					'filter' => 'PartialMatchFilter'
611
				)
612
			),
613
			'FlickrTagTOs()' => array(
614
				'RawValue' => array(
615
					'title' => 'Raw Value',
616
					'filter' => 'PartialMatchFilter'
617
				)
618
			),
619
			'FlickrSetTOs()' => array(
620
				'Title' => array(
621
					'title' => 'Title',
622
					'filter' => 'PartialMatchFilter'
623
				),
624
				'FlickrID' => array(
625
					'title' => 'Flickr ID',
626
					'filter' => 'PartialMatchFilter'
627
				),
628
				'Description' => array(
629
					'title' => 'Description',
630
					'filter' => 'PartialMatchFilter'
631
				)
632
			)
633
		);
634
635
		$this->assertEquals($expected, $fields);
636
	}
637
638
639
	public function testHasOneExistsSearchableToArray() {
640
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
641
		$flickrPhoto->IndexingOff = false;
642
		$flickrPhoto->Title = 'Test title edited';
643
		$photographer = new FlickrAuthorTO();
644
		$photographer->DisplayName = 'Fred Bloggs';
645
		$photographer->PathAlias = '/fredbloggs';
646
647
		$photographer->write();
648
649
		$flickrPhoto->PhotographerID = $photographer->ID; ;
650
		$flickrPhoto->write();
651
		echo 'ID=' . $flickrPhoto->PhotographerID;
652
		$fieldValuesArray = $flickrPhoto->getFieldValuesAsArray();
653
654
		$actual = $fieldValuesArray['Photographer'];
655
		$this->generateAssertionsFromArray($actual);
656
		$expected = array(
657
			'PathAlias' => '/fredbloggs',
658
			'DisplayName' => 'Fred Bloggs',
659
			'FlickrPhotoTO' => '',
660
		);
661
662
		$this->assertEquals($expected, $actual);
663
	}
664
665
666
667
	public function testHasManyExistsSearchableToArray() {
668
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
669
		$flickrPhoto->IndexingOff = false;
670
		$flickrPhoto->Title = 'Test title edited';
671
		$tag1 = new FlickrTagTO();
672
		$tag1->FlickrID = '1000001';
673
		$tag1->Value = 'auckland';
674
		$tag1->RawValue = 'Auckland';
675
		$tag1->write();
676
677
678
		$tag2 = new FlickrTagTO();
679
		$tag2->FlickrID = '1000002';
680
		$tag2->Value = 'wellington';
681
		$tag2->RawValue = 'Wellington';
682
		$tag2->write();
683
684
		$flickrPhoto->FlickrTagTOs()->add($tag1);
685
		$flickrPhoto->FlickrTagTOs()->add($tag2);
686
687
688
689
690
		$flickrPhoto->write();
691
		echo 'ID=' . $flickrPhoto->PhotographerID;
692
		$fieldValuesArray = $flickrPhoto->getFieldValuesAsArray();
693
		$actual = $fieldValuesArray['Photographer'];
694
		$this->assertEquals(array(), $actual);
695
696
697
		$actual = $fieldValuesArray['FlickrTagTOs'];
698
		$this->generateAssertionsFromArrayRecurse($actual);
699
700
		$expected = array(
701
			'0' => array(
702
				'RawValue' => 'Auckland'
703
			),
704
			'1' => array(
705
				'RawValue' => 'Wellington'
706
			)
707
		);
708
709
710
		$this->assertEquals($expected, $actual);
711
	}
712
713
714
	public function testUpdateCMSFieldsDatabject() {
715
		$flickrPhoto = $this->objFromFixture('FlickrPhotoTO', 'photo0001');
716
		$flickrPhoto->IndexingOff = false;
717
		$flickrPhoto->Title = 'Test title edited';
718
		$flickrPhoto->write();
719
		$fields = $flickrPhoto->getCMSFields();
720
721
		$tabset = $fields->findOrMakeTab('Root.ElasticaTerms');
722
		$tabNames = array();
723 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...
724
			$tabFields = array();
725
			foreach($tab->FieldList() as $field) {
726
				array_push($tabFields, $field->getName());
727
			}
728
			$expectedName = 'TermsFor' . $tab->getName(); ;
729
			$expected = array($expectedName);
730
			$this->assertEquals($expected, $tabFields);
731
			array_push($tabNames, $tab->getName());
732
		}
733
		$expected = array('Description', 'Description_shingles', 'Description_standard',
734
			'ShutterSpeed', 'TestMethod', 'TestMethod_shingles', 'TestMethod_standard',
735
			'TestMethodHTML', 'TestMethodHTML_shingles', 'TestMethodHTML_standard',
736
			'Title', 'Title_autocomplete', 'Title_shingles', 'Title_standard');
737
738
		$this->assertEquals($expected, $tabNames);
739
	}
740
741
742
	public function testUpdateCMSFieldsSiteTreeLive() {
743
		echo "+++++++++++++++++ testUpdateCMSFieldsSiteTreeLive +++++++++++++\n";
744
		$page = $this->objFromFixture('SearchableTestPage', 'first');
745
		$page->IndexingOff = false;
746
		$page->Title = 'Test title edited';
747
		$page->write();
748
		$page->doPublish();
749
		$fields = $page->getCMSFields();
750
751
		$tabset = $fields->findOrMakeTab('Root.ElasticaTerms');
752
		$tabNames = array();
753 View Code Duplication
		foreach($tabset->Tabs() as $tab) {
754
			$tabFields = array();
755
			foreach($tab->FieldList() as $field) {
756
				array_push($tabFields, $field->getName());
757
			}
758
			$expectedName = 'TermsFor' . $tab->getName(); ;
759
			$expected = array($expectedName);
760
			$this->assertEquals($expected, $tabFields);
761
			array_push($tabNames, $tab->getName());
762
		}
763
		$expected = array(
764
			'Content', 'Content_standard', 'Link', 'Title', 'Title_autocomplete', 'Title_shingles',
765
			'Title_standard');
766
		$this->generateAssertionsFromArray1D($tabNames);
767
		$this->assertEquals($expected, $tabNames);
768
769
	}
770
771
772 View Code Duplication
	private function getResultsFor($query, $pageLength = 10) {
773
		$es = new ElasticSearcher();
774
		$es->setStart(0);
775
		$es->setPageLength($pageLength);
776
		$es->setClasses('FlickrPhotoTO');
777
		$fields = array('Title' => 1, 'Description' => 1);
778
		$resultList = $es->search($query, $fields)->getList();
779
		$this->assertEquals('SilverStripe\Elastica\ResultList', get_class($resultList));
780
		return $resultList;
781
	}
782
783
}
784