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
8abstract class Search_Index_PaginationTest extends PHPUnit_Framework_TestCase
9{
10	protected $index;
11
12	function testNoPagingRequired()
13	{
14		$this->assertResultCorrect(15, 0, 25, 1, 15);
15	}
16
17	function testGetSecondPage()
18	{
19		$this->assertResultCorrect(30, 10, 10, 11, 20);
20	}
21
22	private function assertResultCorrect($count, $from, $perPage, $first, $last)
23	{
24		$this->addDocuments($count);
25
26		$query = new Search_Query;
27		$query->setOrder('object_id_nasc');
28		$query->filterType('article');
29		$query->setRange($from, $perPage);
30
31		$result = $query->search($this->index);
32
33		$this->assertEquals($count, count($result), 'total count');
34
35		$real = [];
36		foreach ($result as $hit) {
37			$real[] = $hit;
38		}
39
40		$this->assertEquals($first, $real[0]['object_id'], 'first entry');
41		$this->assertEquals($last, $real[count($real) - 1]['object_id'], 'last entry');
42	}
43
44	private function addDocuments($count)
45	{
46		$index = $this->index;
47
48		$typeFactory = $index->getTypeFactory();
49
50		for ($i = 0; $count > $i; ++$i) {
51			$index->addDocument(
52				[
53					'object_type' => $typeFactory->identifier('article'),
54					'object_id' => $typeFactory->identifier($i + 1),
55				]
56			);
57		}
58	}
59}
60