1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8/**
9 * @group unit
10 */
11abstract class Search_Index_SortTest extends PHPUnit_Framework_TestCase
12{
13	protected $index;
14
15	protected function populate($index)
16	{
17		$this->add($index, 'A', '1', 'Hello', 'Hello Foobar');
18		$this->add($index, 'B', '10', 'foobar', 'Hello World Foobar');
19		$this->add($index, 'C', '2', 'Baz', 'Baz');
20	}
21
22	function sortCases()
23	{
24		return [
25			['numeric_field_nasc', 'ACB'],
26			['numeric_field_ndesc', 'BCA'],
27			['numeric_field_asc', 'ABC'],
28			['text_field_asc', 'CBA'],
29			['text_field_desc', 'ABC'],
30			['other_field_asc', 'CAB'],
31			['other_field_desc', 'BAC'],
32			['object_id_asc', 'ABC'],
33			['object_id_desc', 'CBA'],
34		];
35	}
36
37	/**
38	 * @dataProvider sortCases
39	 */
40	function testOrdering($mode, $expected)
41	{
42		$query = new Search_Query;
43		$query->filterType('wiki page');
44		$query->setOrder($mode);
45
46		$results = $query->search($this->index);
47
48		$this->assertOrderIs($expected, $results);
49	}
50
51	function testWeightImpact()
52	{
53		$query = new Search_Query;
54		$query->setWeightCalculator(
55			new Search_Query_WeightCalculator_Field(
56				[
57					'text_field' => 100,
58					'other_field' => 0.0001,
59				]
60			)
61		);
62		$query->filterContent('foobar', ['text_field', 'other_field']);
63
64		$results = $query->search($this->index);
65
66		$this->assertOrderIs('BA', $results);
67	}
68
69	function returnOnlyCases()
70	{
71		return [
72			[[1, 2, 3, 4, 5, 6], 'ACB'],
73			[[1, 2, 3], 'ACB'],
74			[[], 'ACB'],
75			[[1, 3], 'AB'],
76			[[3, 2, 1], 'BCA'],
77			[[3], 'B'],
78			[[3, 1], 'BA'],
79			[[4, 5, 6], ''],
80		];
81	}
82
83	/**
84	 * @dataProvider returnOnlyCases
85	 *
86	 * @param array $returnOnlyValue
87	 * @param string $expected
88	 */
89	function testReturnOnly($returnOnlyValue, $expected)
90	{
91		$query = new Search_Query;
92		$query->filterType('wiki page');
93		$query->setOrder('numeric_field_nasc');
94		$query->setReturnOnlyResultList($returnOnlyValue);
95
96		$results = $query->search($this->index);
97
98		$this->assertOrderIs($expected, $results);
99	}
100
101	private function assertOrderIs($expected, $results)
102	{
103		$str = '';
104		foreach ($results as $row) {
105			$str .= $row['object_id'];
106		}
107
108		$this->assertEquals($expected, $str);
109	}
110
111	private function add($index, $page, $numeric, $text, $text2)
112	{
113		$typeFactory = $index->getTypeFactory();
114
115		$index->addDocument(
116			[
117				'object_type' => $typeFactory->identifier('wiki page'),
118				'object_id' => $typeFactory->identifier($page),
119				'numeric_field' => $typeFactory->sortable($numeric),
120				'text_field' => $typeFactory->sortable($text),
121				'other_field' => $typeFactory->sortable($text2),
122			]
123		);
124	}
125}
126