testSavedSearches.php
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
require_once (KT_DIR . '/tests/test.php');
require_once (KT_DIR . '/ktapi/ktapi.inc.php');
/**
* These are the unit tests for the main KTAPI class
*
*/
class savedSearchTestCase extends KTUnitTestCase {
/**
* @var object $ktapi The main ktapi object
*/
var $ktapi;
/**
* @var object $savedSearch The saved searches object object
*/
var $savedSearch;
/**
* @var object $session The KT session object
*/
var $session;
/**
* @var object $root The KT folder object
*/
var $root;
/**
* This method sets up the KT session
*
*/
public function setUp() {
$this->ktapi = new KTAPI();
$this->savedSearch = new savedSearches($this->ktapi);
$this->session = $this->ktapi->start_session('admin', 'admin');
$this->root = $this->ktapi->get_root_folder();
$this->assertTrue($this->root instanceof KTAPI_Folder);
}
/**
* This method emds the KT session
*
*/
public function tearDown() {
$this->session->logout();
}
/**
* This method tests the creation of the saved search
*
*/
public function testCreate()
{
//case 1: user logged in
$searchID = $this->savedSearch->create('test_search', '(GeneralText contains "title")');
$this->assertNotA($searchID, 'PEAR_Error');
$this->assertNotNull($searchID);
$this->assertNoErrors();
$this->savedSearch->delete($searchID);
//case 2: user NOT logged in
$this->ktapi->session_logout();
$searchID = $this->savedSearch->create('test_search', '(GeneralText contains "title")');
$this->assertIsA($searchID, 'PEAR_Error');
$this->assertNoErrors();
}
/**
* This method tests the retrieval for the saved search by it's id
*
*/
public function testGetSavedSearch()
{
// case 1: search exists
$searchID = $this->savedSearch->create('test_search', '(GeneralText contains "title")');
$list = $this->savedSearch->getList();
foreach($list as $item){
if($item['id'] == $searchID){
$search = $item['id'];
break;
}
}
$savedSearch = $this->savedSearch->getSavedSearch($search);
$this->assertNotNull($savedSearch);
$this->assertNoErrors();
$this->savedSearch->delete($searchID);
// case 2: search does NOT exists
$list = $this->savedSearch->getList();
$inList = FALSE;
foreach($list as $item){
if($item['id'] == $searchID){
$inList = TRUE;
break;
}
}
$this->assertNotA($list, 'PEAR_Error');
$this->assertFalse($inList);
$this->assertNoErrors();
}
/**
* This method tests the list of the saved search
*
*/
public function testList()
{
// case 1: Saved searches exist
$array = array();
$searchID = $this->savedSearch->create('test_search', '(GeneralText contains "title")');
$list = $this->savedSearch->getList();
$this->assertNotA($list, 'PEAR_Error');
$this->assertNotEqual($list, $array);
$this->assertNoErrors();
$this->savedSearch->delete($searchID);
// case 2: saved search does NOT exist
$list = $this->savedSearch->getList();
$inList = FALSE;
foreach($list as $item){
if($item['id'] == $searchID){
$inList = TRUE;
break;
}
}
$this->assertNotA($list, 'PEAR_Error');
$this->assertFalse($inList);
$this->assertNoErrors();
}
/**
* This method tests the deleting of the saved search
*
*/
public function testDelete()
{
$searchID = $this->savedSearch->create('test_search', '(GeneralText contains "title")');
$this->savedSearch->delete($searchID);
$result = $this->savedSearch->getSavedSearch($searchID);
$array = array();
$this->assertEqual($result, $array);
$this->assertNotA($result, 'PEAR_Error');
$this->assertNoErrors();
}
/**
* This method tests the processing of the saved search
*
*/
public function testRunSavedSearch()
{
// create the document object
$randomFile = $this->createRandomFile();
$document = $this->root->add_document('title.txt', 'name_1.txt', 'Default', $randomFile);
@unlink($randomFile);
$searchID = $this->savedSearch->create('test_search', '(GeneralText contains "title")');
$result = $this->savedSearch->runSavedSearch($searchID);
$this->assertNotNull($result);
$this->assertNotA($result, 'PEAR_Error');
$this->assertNoErrors();
$document->delete('Testing');
$document->expunge();
$this->savedSearch->delete($searchID);
}
function createRandomFile($content = 'this is some text') {
$temp = tempnam(dirname(__FILE__), 'myfile');
$fp = fopen($temp, 'wt');
fwrite($fp, $content);
fclose($fp);
return $temp;
}
}
?>