| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use SilverStripe\Elastica\ElasticaUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Test the functionality of ElasticaUtil class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package elastica | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class ElasticaUtilTest extends ElasticsearchBaseTest { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	public function testHumanReadableFalse() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		$this->assertEquals('No', ElasticaUtil::showBooleanHumanReadable(false)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	public function testHumanReadableTrue() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		$this->assertEquals('No', ElasticaUtil::showBooleanHumanReadable(false)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 20 |  | View Code Duplication | 	public function testPairOfConsecutiveIncorrectWords() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 		$sa = $this->getSuggestionArray('New Zealind raalway', | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 			'new zealand railway', | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 			'new <strong class="hl">zealand railway</strong>'); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		$pair = ElasticaUtil::getPhraseSuggestion($sa); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		$expected = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 			'suggestedQuery' => 'New Zealand railway', | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand railway</strong>' | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 		$this->assertEquals($expected, $pair); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 34 |  | View Code Duplication | 	public function testBooleanQuery() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$sa = $this->getSuggestionArray('New Zealind AND sheep', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			'new zealand and sheep', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			'new <strong class="hl">zealand</strong> and sheep'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$pair = ElasticaUtil::getPhraseSuggestion($sa); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$expected = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			'suggestedQuery' => 'New Zealand AND sheep', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand</strong> AND sheep' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$this->assertEquals($expected, $pair); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 47 |  | View Code Duplication | 	public function testOneIncorrectWord() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$sa = $this->getSuggestionArray('New Zealind', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			'new zealand', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			'new <strong class="hl">zealand</strong>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$pair = ElasticaUtil::getPhraseSuggestion($sa); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$expected = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			'suggestedQuery' => 'New Zealand', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			'suggestedQueryHighlighted' => 'New <strong class="hl">Zealand</strong>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->assertEquals($expected, $pair); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 60 |  | View Code Duplication | 	public function testOneIncorrectWordLowerCase() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$sa = $this->getSuggestionArray('new zealind', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			'new zealand', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			'new <strong class="hl">zealand</strong>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$pair = ElasticaUtil::getPhraseSuggestion($sa); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$expected = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			'suggestedQuery' => 'new zealand', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			'suggestedQueryHighlighted' => 'new <strong class="hl">zealand</strong>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$this->assertEquals($expected, $pair); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	public function testNoSuggestionsRequired() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$suggestion = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			'text' => 'New Zealand', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			'offset' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			'length' => 11, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			'options' => array() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$sa = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		array_push($sa, $suggestion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$expected = array(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$pair = ElasticaUtil::getPhraseSuggestion($sa); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$this->assertEquals(null, $pair); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * Test parsing no terms suggested | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	public function testParseExplanationNoTerms() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		#FIXME Possibly flickrphotoTO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$explanation = "() -ConstantScore(_uid:FlickrPhoto#7369)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$expected = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$terms = ElasticaUtil::parseSuggestionExplanation($explanation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$this->assertEquals($expected, $terms); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	public function testParseExplanationDoubleStartingBracket() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$explanation = "((Title.standard:wellington Title.standard:view Description.standard:new "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$explanation .= "Description.standard:zealand Description.standard:wellington Description.standard:view Description.standard:including Description.standard:buildings Description.standard:exhibition Description.standard:aerial)~3) -ConstantScore(_uid:FlickrPhoto#3079)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$terms = ElasticaUtil::parseSuggestionExplanation($explanation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$termsDescription = $terms['Description.standard']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		sort($termsDescription); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$termsTitle = $terms['Title.standard']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		sort($termsTitle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$expected = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			'Title.standard' => $termsTitle, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			'Description.standard' => $termsDescription | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$terms = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			'Title.standard' => array('view', 'wellington'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			'Description.standard' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 				'aerial', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 				'buildings', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 				'exhibition', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				'including', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 				'new', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 				'view', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 				'wellington', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				'zealand' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		$this->assertEquals($expected, $terms); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	public function testParseExplanationNoLeadingBrackets() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		$explanation = "Title.standard:new -ConstantScore(_uid:FlickrPhotoTO#76)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		$terms = ElasticaUtil::parseSuggestionExplanation($explanation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$expected = array('Title.standard' => array('new')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$this->assertEquals($expected, $terms); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	public function testParseExplanationBracketsShowing() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		$explanation = '(Description.standard:bay Description.standard:tram Description.standard:manners) -ConstantScore(_uid:FlickrPhoto#4645)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$terms = ElasticaUtil::parseSuggestionExplanation($explanation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		$expected = array('Description.standard' => array('bay', 'tram', 'manners')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$this->assertEquals($expected, $terms); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * Simulate a call to Elastica to get suggestions for a given phrase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @return [type] [description] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	private function getSuggestionArray($phrase, $suggestion, $highlightedSuggestion) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$result = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$suggest1 = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		$suggest1['text'] = $phrase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$suggest1['offset'] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		$suggest1['length'] = strlen($phrase); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		$options = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$option0 = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$option0['text'] = $suggestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		$option0['highlighted'] = $highlightedSuggestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		//For completeness, currently not used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		$option0['score'] = 9.0792E-5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$options[0] = $option0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		$suggest1['options'] = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		array_push($result, $suggest1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                        
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.