/Search/tests/session_test.php

https://github.com/xmak/ezComponents · PHP · 807 lines · 654 code · 130 blank · 23 comment · 2 complexity · 56c67e18a20de4f91c9bbfe19c5b3a2a MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  4. * @license http://ez.no/licenses/new_bsd New BSD License
  5. * @version //autogentag//
  6. * @filesource
  7. * @package Search
  8. * @subpackage Tests
  9. */
  10. require_once 'testfiles/article.php';
  11. require_once 'testfiles/test-classes.php';
  12. /**
  13. * Test the handler classes.
  14. *
  15. * @package Search
  16. * @subpackage Tests
  17. */
  18. class ezcSearchSessionTest extends ezcTestCase
  19. {
  20. public static function suite()
  21. {
  22. return new PHPUnit_Framework_TestSuite( "ezcSearchSessionTest" );
  23. }
  24. public function setUp()
  25. {
  26. try
  27. {
  28. $this->backend = new ezcSearchSolrHandler;
  29. }
  30. catch ( ezcSearchCanNotConnectException $e )
  31. {
  32. self::markTestSkipped( 'Solr is not running.' );
  33. }
  34. $this->testFilesDir = dirname( __FILE__ ) . '/testfiles/';
  35. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
  36. '<delete><query>timestamp:[* TO *]</query></delete>' );
  37. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
  38. '<commit/>' );
  39. }
  40. public function testCreateSession()
  41. {
  42. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  43. }
  44. public function testSetProperties()
  45. {
  46. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  47. self::assertSetPropertyFails( $session, 'definitionManager', array( 'foo' ) );
  48. self::assertSetPropertyFails( $session, 'handler', array( 'foo' ) );
  49. self::assertSetPropertyFails( $session, 'doesNotExist', array( 'foo' ) );
  50. }
  51. public function testGetProperties()
  52. {
  53. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  54. self::assertEquals( 'ezcSearchSolrHandler', get_class( $session->handler ) );
  55. self::assertEquals( 'ezcSearchXmlManager', get_class( $session->definitionManager ) );
  56. try
  57. {
  58. $session->doesNotExist;
  59. self::fail( 'Expected exception was not thrown.' );
  60. }
  61. catch ( ezcBasePropertyNotFoundException $e )
  62. {
  63. self::assertEquals( "No such property name 'doesNotExist'.", $e->getMessage() );
  64. }
  65. }
  66. public function testBrokenState()
  67. {
  68. $a = new Article( null, 'Test Article', 'This is an article to test', 'the body of the article', time() );
  69. $a->omitStateElement( 'title' );
  70. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  71. try
  72. {
  73. $session->index( $a );
  74. self::fail( 'Expected exeption not thrown.' );
  75. }
  76. catch ( ezcSearchIncompleteStateException $e )
  77. {
  78. self::assertEquals( "The getState() method did not return any value for the field 'title'.", $e->getMessage() );
  79. }
  80. }
  81. public function testIndexDocument1()
  82. {
  83. $a = new Article( null, 'Test Article', 'This is an article to test', 'the body of the article', time() );
  84. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  85. $session->index( $a );
  86. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), '<commit/>' );
  87. $q = $session->createFindQuery( 'Article' );
  88. $q->where( $q->eq( 'title', 'Article' ) );
  89. $r = $session->find( $q );
  90. self::assertEquals( 1, $r->resultCount );
  91. }
  92. public function testIndexDocument2()
  93. {
  94. $a = new Article( null, 'Test Article', 'This is an article to test', 'the body of the article', time() );
  95. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  96. $session->beginTransaction();
  97. for ( $i = 0; $i < 100; $i++ )
  98. {
  99. $session->index( $a );
  100. }
  101. $session->commit();
  102. $q = $session->createFindQuery( 'Article' );
  103. $q->where( $q->eq( 'title', 'Article' ) );
  104. $r = $session->find( $q );
  105. self::assertEquals( 1, $r->resultCount );
  106. }
  107. public function testIndexDocument3()
  108. {
  109. $d = file_get_contents( dirname( __FILE__ ) . '/../../../docs/guidelines/implementation.txt' );
  110. $a = new Article( null, 'Test Article', 'This is Rethans an article to test', $d, time(), array( 'Derick Rethans', 'Legolas Rethans' ) );
  111. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  112. $session->index( $a );
  113. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), '<commit/>' );
  114. $q = $session->createFindQuery( 'Article' );
  115. $q->where( $q->eq( 'author', 'Derick Rethans' ) );
  116. $r = $session->find( $q );
  117. self::assertEquals( 1, $r->resultCount );
  118. }
  119. public function testIndexDocumentWithBrokenUtf8()
  120. {
  121. $a = new Article( null, 'Test Article', file_get_contents( dirname( __FILE__ ) . '/testfiles/issue15623-broken-utf8.xml' ), 'This is an article to test', time() );
  122. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  123. $session->index( $a );
  124. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), '<commit/>' );
  125. $q = $session->createFindQuery( 'Article' );
  126. $q->where( $q->eq( 'summary', 'ferences' ) );
  127. $r = $session->find( $q );
  128. self::assertEquals( 1, $r->resultCount );
  129. }
  130. public function testUpdateDocument1()
  131. {
  132. $a = new Article( null, 'Test Article', 'This is an article to test', 'the body of the article', time() );
  133. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  134. $session->index( $a );
  135. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), '<commit/>' );
  136. $q = $session->createFindQuery( 'Article' );
  137. $q->where( $q->eq( 'title', 'Article' ) );
  138. $r = $session->find( $q );
  139. self::assertEquals( 1, $r->resultCount );
  140. self::assertEquals( 'Test Article', $r->documents[$a->id]->document->title );
  141. self::assertEquals( 'This is an article to test', $r->documents[$a->id]->document->summary );
  142. $a->title = "New Title";
  143. $session->update( $a );
  144. $q = $session->createFindQuery( 'Article' );
  145. $q->where( $q->eq( 'title', 'Title' ) );
  146. $r = $session->find( $q );
  147. self::assertEquals( 1, $r->resultCount );
  148. self::assertEquals( 'New Title', $r->documents[$a->id]->document->title );
  149. self::assertEquals( 'This is an article to test', $r->documents[$a->id]->document->summary );
  150. }
  151. public function testCreateFindQuery1()
  152. {
  153. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  154. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  155. $session->index( $a );
  156. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  157. $session->index( $a );
  158. $q = $session->createFindQuery( 'Article' );
  159. $q->where( $q->eq( 'body', 'Article' ) )->where( $q->eq( 'title', 'Test' ) );
  160. $r = $session->find( $q );
  161. self::assertEquals( 2, $r->resultCount );
  162. self::assertEquals( 2, count( $r->documents ) );
  163. }
  164. public function testCreateFindQueryWithAccent()
  165. {
  166. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  167. $a = new Article( null, 'Test Article Eén', 'This is article Eén to test', 'the body of the article', time() );
  168. $session->index( $a );
  169. $a = new Article( null, 'Test Article Twee', 'This is article twee to test', 'the body of the article', time() );
  170. $session->index( $a );
  171. $q = $session->createFindQuery( 'Article' );
  172. $q->where( $q->eq( 'summary', 'Eén' ) );
  173. $r = $session->find( $q );
  174. self::assertEquals( 1, $r->resultCount );
  175. }
  176. public function testCreateFindQueryJapanese()
  177. {
  178. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  179. $a = new Article( null, 'Test Article Eén', 'マリコ', 'the body of the article', time() );
  180. $session->index( $a );
  181. $a = new Article( null, 'Test Article Twee', 'ソウシ', 'the body of the article', time() );
  182. $session->index( $a );
  183. $q = $session->createFindQuery( 'Article' );
  184. $q->where( $q->eq( 'summary', 'ソウ*' ) );
  185. $r = $session->find( $q );
  186. self::assertEquals( 1, $r->resultCount );
  187. }
  188. public function testCreateFindQueryOr()
  189. {
  190. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  191. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  192. $session->index( $a );
  193. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  194. $session->index( $a );
  195. $q = $session->createFindQuery( 'Article' );
  196. $q->where( $q->lOr( $q->eq( 'title', 'Nul' ), $q->eq( 'title', 'Drie' ) ) );
  197. $r = $session->find( $q );
  198. self::assertEquals( 0, $r->resultCount );
  199. $q = $session->createFindQuery( 'Article' );
  200. $q->where( $q->lOr( $q->eq( 'title', 'Eén' ), $q->eq( 'title', 'Drie' ) ) );
  201. $r = $session->find( $q );
  202. self::assertEquals( 1, $r->resultCount );
  203. $q = $session->createFindQuery( 'Article' );
  204. $q->where( $q->lOr( $q->eq( 'title', 'Twee' ), $q->eq( 'title', 'Drie' ) ) );
  205. $r = $session->find( $q );
  206. self::assertEquals( 1, $r->resultCount );
  207. $q = $session->createFindQuery( 'Article' );
  208. $q->where( $q->lOr( $q->eq( 'title', 'Eén' ), $q->eq( 'title', 'Twee' ) ) );
  209. $r = $session->find( $q );
  210. self::assertEquals( 2, $r->resultCount );
  211. }
  212. public function testCreateFindQueryOneOr()
  213. {
  214. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  215. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  216. $session->index( $a );
  217. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  218. $session->index( $a );
  219. $q = $session->createFindQuery( 'Article' );
  220. $q->where( $q->lOr( $q->eq( 'title', 'Twee' ) ) );
  221. $r = $session->find( $q );
  222. self::assertEquals( 1, $r->resultCount );
  223. }
  224. public function testCreateFindQueryEmptyOr()
  225. {
  226. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  227. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  228. $session->index( $a );
  229. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  230. $session->index( $a );
  231. $q = $session->createFindQuery( 'Article' );
  232. try
  233. {
  234. $q->where( $q->lOr() );
  235. self::fail( 'Expected exception was not thrown.' );
  236. }
  237. catch ( ezcSearchQueryVariableParameterException $e )
  238. {
  239. self::assertEquals( "The method 'lOr' expected at least 1 parameter but none were provided.", $e->getMessage() );
  240. }
  241. }
  242. public function testCreateFindQueryOneAnd()
  243. {
  244. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  245. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  246. $session->index( $a );
  247. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  248. $session->index( $a );
  249. $q = $session->createFindQuery( 'Article' );
  250. $q->where( $q->lAnd( $q->eq( 'title', 'Twee' ) ) );
  251. $r = $session->find( $q );
  252. self::assertEquals( 1, $r->resultCount );
  253. }
  254. public function testCreateFindQueryAnd()
  255. {
  256. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  257. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  258. $session->index( $a );
  259. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  260. $session->index( $a );
  261. $q = $session->createFindQuery( 'Article' );
  262. $q->where( $q->lAnd( $q->eq( 'title', 'Twee' ), $q->eq( 'summary', 'second' ) ) );
  263. $r = $session->find( $q );
  264. self::assertEquals( 1, $r->resultCount );
  265. }
  266. public function testCreateFindQueryEmptyAnd()
  267. {
  268. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  269. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() );
  270. $session->index( $a );
  271. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  272. $session->index( $a );
  273. $q = $session->createFindQuery( 'Article' );
  274. try
  275. {
  276. $q->where( $q->lAnd() );
  277. self::fail( 'Expected exception was not thrown.' );
  278. }
  279. catch ( ezcSearchQueryVariableParameterException $e )
  280. {
  281. self::assertEquals( "The method 'lAnd' expected at least 1 parameter but none were provided.", $e->getMessage() );
  282. }
  283. }
  284. public function testCreateFindQueryNot()
  285. {
  286. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  287. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  288. $session->index( $a );
  289. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  290. $session->index( $a );
  291. $q = $session->createFindQuery( 'Article' );
  292. $q->where( $q->not( $q->eq( 'title', 'Twee' ) ) );
  293. $r = $session->find( $q );
  294. self::assertEquals( 1, $r->resultCount );
  295. }
  296. public function testCreateFindQueryImportant()
  297. {
  298. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  299. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  300. $session->index( $a );
  301. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  302. $session->index( $a );
  303. $q = $session->createFindQuery( 'Article' );
  304. $q->where( $q->important( $q->eq( 'title', 'Twee' ) ) );
  305. self::assertEquals( "q=ezcsearch_type_s%3AArticle+AND+%2Btitle_t%3ATwee%5E2&wt=json&df=&fl=score+id_s+title_t+summary_t+published_l+author_s+number_l+ezcsearch_type_s+score&start=0&rows=999999", $q->getQuery() );
  306. }
  307. public function testCreateFindQueryBoost()
  308. {
  309. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  310. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  311. $session->index( $a );
  312. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  313. $session->index( $a );
  314. $q = $session->createFindQuery( 'Article' );
  315. $q->where( $q->boost( $q->eq( 'title', 'Twee' ), 2.5 ) );
  316. self::assertEquals( "q=ezcsearch_type_s%3AArticle+AND+title_t%3ATwee%5E2.5&wt=json&df=&fl=score+id_s+title_t+summary_t+published_l+author_s+number_l+ezcsearch_type_s+score&start=0&rows=999999", $q->getQuery() );
  317. }
  318. public function testCreateFindQueryFuzz()
  319. {
  320. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  321. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  322. $session->index( $a );
  323. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  324. $session->index( $a );
  325. $q = $session->createFindQuery( 'Article' );
  326. $q->where( $q->fuzz( $q->eq( 'title', 'Twee' ) ) );
  327. self::assertEquals( "q=ezcsearch_type_s%3AArticle+AND+title_t%3ATwee%5E2%7E&wt=json&df=&fl=score+id_s+title_t+summary_t+published_l+author_s+number_l+ezcsearch_type_s+score&start=0&rows=999999", $q->getQuery() );
  328. }
  329. public function testCreateFindQueryFuzzWithFactor()
  330. {
  331. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  332. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  333. $session->index( $a );
  334. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  335. $session->index( $a );
  336. $q = $session->createFindQuery( 'Article' );
  337. $q->where( $q->fuzz( $q->eq( 'title', 'Twee' ), 2.5 ) );
  338. self::assertEquals( "q=ezcsearch_type_s%3AArticle+AND+title_t%3ATwee%5E2%7E2.5&wt=json&df=&fl=score+id_s+title_t+summary_t+published_l+author_s+number_l+ezcsearch_type_s+score&start=0&rows=999999", $q->getQuery() );
  339. }
  340. public function testDeleteById()
  341. {
  342. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  343. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  344. $session->index( $a );
  345. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  346. $session->index( $a );
  347. $q = $session->createFindQuery( 'Article' );
  348. $r = $session->find( $q );
  349. self::assertEquals( 2, $r->resultCount );
  350. self::assertEquals( 2, count( $r->documents ) );
  351. self::assertEquals( '4822deec4153d-one', $r->documents['4822deec4153d-one']->document->id );
  352. self::assertEquals( '4822deec4153d-two', $r->documents['4822deec4153d-two']->document->id );
  353. $session->deleteById( '4822deec4153d-one', 'Article' );
  354. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), '<commit/>' );
  355. $q = $session->createFindQuery( 'Article' );
  356. $r = $session->find( $q );
  357. self::assertEquals( 1, $r->resultCount );
  358. self::assertEquals( 1, count( $r->documents ) );
  359. self::assertFalse( isset( $r->documents['4822deec4153d-one'] ) );
  360. self::assertEquals( '4822deec4153d-two', $r->documents['4822deec4153d-two']->document->id );
  361. }
  362. public function testFindById()
  363. {
  364. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  365. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  366. $session->index( $a );
  367. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  368. $session->index( $a );
  369. $r = $session->findById( '4822deec4153d-one', 'Article' );
  370. self::assertequals( '4822deec4153d-one', $r->document->id );
  371. $r = $session->findById( '4822deec4153d-two', 'Article' );
  372. self::assertEquals( '4822deec4153d-two', $r->document->id );
  373. }
  374. public function testDeleteByIdNonExisting()
  375. {
  376. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  377. $session->deleteById( '4822deec4153d-three', 'Article' );
  378. }
  379. public function testFindByIdNonExisting()
  380. {
  381. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  382. try
  383. {
  384. $r = $session->findById( '4822deec4153d-three', 'Article' );
  385. self::fail( 'Expected exception not thrown.' );
  386. }
  387. catch ( ezcSearchIdNotFoundException $e )
  388. {
  389. self::assertEquals( "There is no document with ID '4822deec4153d-three'.", $e->getMessage() );
  390. }
  391. }
  392. public function testCreateDeleteQuery()
  393. {
  394. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  395. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  396. $session->index( $a );
  397. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  398. $session->index( $a );
  399. $q = $session->createDeleteQuery( 'Article' );
  400. $q->where( $q->eq( 'title', 'Twee' ) );
  401. self::assertEquals( "<delete><query>ezcsearch_type_s:Article AND title_t:Twee</query></delete>", $q->getQuery() );
  402. }
  403. public function testCreateFindQueryLimit()
  404. {
  405. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  406. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  407. $session->index( $a );
  408. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  409. $session->index( $a );
  410. $q = $session->createFindQuery( 'Article' );
  411. $q->where( $q->eq( 'title', 'Article' ) );
  412. $q->limit( 1 );
  413. $r = $session->find( $q );
  414. self::assertEquals( 2, $r->resultCount );
  415. self::assertEquals( 1, count( $r->documents ) );
  416. self::assertEquals( '4822deec4153d-one', $r->documents['4822deec4153d-one']->document->id );
  417. }
  418. public function testCreateFindQueryLimitOffset()
  419. {
  420. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  421. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  422. $session->index( $a );
  423. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  424. $session->index( $a );
  425. $q = $session->createFindQuery( 'Article' );
  426. $q->where( $q->eq( 'title', 'Article' ) );
  427. $q->limit( 1, 1 );
  428. $r = $session->find( $q );
  429. self::assertEquals( 2, $r->resultCount );
  430. self::assertEquals( 1, count( $r->documents ) );
  431. self::assertEquals( '4822deec4153d-two', $r->documents['4822deec4153d-two']->document->id );
  432. }
  433. public function testCreateFindQueryOrderByDefault()
  434. {
  435. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  436. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  437. $session->index( $a );
  438. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  439. $session->index( $a );
  440. $q = $session->createFindQuery( 'Article' );
  441. $q->where( $q->eq( 'title', 'Article' ) );
  442. $q->limit( 1 );
  443. $q->orderBy( 'id' );
  444. $r = $session->find( $q );
  445. self::assertEquals( 2, $r->resultCount );
  446. self::assertEquals( 1, count( $r->documents ) );
  447. self::assertEquals( 'Test Article Eén', $r->documents['4822deec4153d-one']->document->title );
  448. }
  449. public function testCreateFindQueryOrderByAsc()
  450. {
  451. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  452. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  453. $session->index( $a );
  454. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  455. $session->index( $a );
  456. $q = $session->createFindQuery( 'Article' );
  457. $q->where( $q->eq( 'title', 'Article' ) );
  458. $q->limit( 1 );
  459. $q->orderBy( 'id' );
  460. $r = $session->find( $q );
  461. self::assertEquals( 2, $r->resultCount );
  462. self::assertEquals( 1, count( $r->documents ) );
  463. self::assertEquals( 'Test Article Eén', $r->documents['4822deec4153d-one']->document->title );
  464. }
  465. public function testCreateFindQueryOrderByDesc()
  466. {
  467. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  468. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  469. $session->index( $a );
  470. $a = new Article( '4822deec4153d-two', 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  471. $session->index( $a );
  472. $q = $session->createFindQuery( 'Article' );
  473. $q->where( $q->eq( 'title', 'Article' ) );
  474. $q->limit( 1 );
  475. $q->orderBy( 'title', ezcSearchQueryTools::DESC );
  476. $r = $session->find( $q );
  477. self::assertEquals( 2, $r->resultCount );
  478. self::assertEquals( 1, count( $r->documents ) );
  479. self::assertEquals( 'Test Article Twee', $r->documents['4822deec4153d-two']->document->title );
  480. }
  481. public function testCreateFindQueryWrongField()
  482. {
  483. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  484. $a = new Article( '4822deec4153d-one', 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  485. $session->index( $a );
  486. $q = $session->createFindQuery( 'Article' );
  487. try
  488. {
  489. $q->where( $q->eq( 'wrong-field', 'Article' ) );
  490. self::fail( 'Expected exception was not thrown.' );
  491. }
  492. catch ( ezcSearchFieldNotDefinedException $e )
  493. {
  494. self::assertEquals( "The document type 'Article' does not define the field 'wrong-field'.", $e->getMessage() );
  495. }
  496. }
  497. public function testCreateFindQueryFacet()
  498. {
  499. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  500. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400, "derick" );
  501. $session->index( $a );
  502. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time(), "alexandru" );
  503. $session->index( $a );
  504. $a = new Article( null, 'Test Article Drie', 'This is the third article to test', 'the body of the article', time(), "derick" );
  505. $session->index( $a );
  506. $q = $session->createFindQuery( 'Article' );
  507. $q->facet( 'author' );
  508. $r = $session->find( $q );
  509. self::assertEquals( 3, $r->resultCount );
  510. self::assertEquals( 3, count( $r->documents ) );
  511. self::assertEquals( array( 'alexandru' => 1, 'derick' => 2 ), $r->facets['author'] );
  512. }
  513. public function testCreateFindQueryBetween()
  514. {
  515. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  516. $time1 = time() - 86400;
  517. $time2 = time();
  518. $time3 = time() + 3600;
  519. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', $time1, "derick" );
  520. $session->index( $a );
  521. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', $time2, "alexandru" );
  522. $session->index( $a );
  523. $a = new Article( null, 'Test Article Drie', 'This is the third article to test', 'the body of the article', $time3, "derick" );
  524. $session->index( $a );
  525. $q = $session->createFindQuery( 'Article' );
  526. $timeb1 = time() - 20;
  527. $timeb2 = time() + 7200;
  528. $q->where( $q->between( 'published', $timeb1, $timeb2 ) );
  529. $r = $session->find( $q );
  530. self::assertEquals( 2, $r->resultCount );
  531. self::assertEquals( 2, count( $r->documents ) );
  532. self::assertEquals( "q=ezcsearch_type_s%3AArticle+AND+published_l%3A%5B$timeb1+TO+$timeb2%5D&wt=json&df=&fl=score+id_s+title_t+summary_t+published_l+author_s+number_l+ezcsearch_type_s+score&start=0&rows=999999", $q->getQuery() );
  533. }
  534. public function testDeleteQuery()
  535. {
  536. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  537. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time() - 86400 );
  538. $session->index( $a );
  539. $a = new Article( null, 'Test Article Twee', 'This is the second article to test', 'the body of the article', time() );
  540. $session->index( $a );
  541. $q = $session->createFindQuery( 'Article' );
  542. $r = $session->find( $q );
  543. self::assertEquals( 2, $r->resultCount );
  544. $q = $session->createDeleteQuery( 'Article' );
  545. $q->where( $q->eq( 'title', 'Twee' ) );
  546. $session->delete( $q );
  547. $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), '<commit/>' );
  548. $q = $session->createFindQuery( 'Article' );
  549. $r = $session->find( $q );
  550. self::assertEquals( 1, $r->resultCount );
  551. }
  552. public function testCreateFindWithPhrase()
  553. {
  554. $session = new ezcSearchSession( $this->backend, new ezcSearchXmlManager( $this->testFilesDir ) );
  555. $a = new Article( null, 'Test Article Eén', 'This is the first article to test', 'the body of the article', time(), array( "Me", "You", "Everybody" ) );
  556. $session->index( $a );
  557. $q = $session->createFindQuery( 'Article' );
  558. $q->where( $q->eq( 'title', 'Test Article' ) );
  559. $r = $session->find( $q );
  560. self::assertEquals( 1, $r->resultCount );
  561. }
  562. public static function datatypes()
  563. {
  564. return array(
  565. array( 'test', '<b>test</b>', true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'id', 'testId' ),
  566. array( 'test', '<b>test</b>', false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'id', 'testId' ),
  567. array( 'test', '<b>test</b>', true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'string', 'test' ),
  568. array( 'test', '<b>test</b>', false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', 'test' ),
  569. array( 'test', '<b>test</b>', true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'string', 'tes*' ),
  570. array( 'test', '<b>test</b>', false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', 'tes*' ),
  571. array( 'test', '<b>test</b>', false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', '<b>test</b>' ),
  572. array( 'test', '<b>test</b>', true, -42, -12831e4, "2008-04-23 16:35 Europe/Oslo", 'bool', true ),
  573. array( 'test', '<b>test</b>', false, -42, -0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'bool', false ),
  574. array( 'test', '<b>test</b>', true, -51, -12831e4, "2008-04-23 16:35 Europe/Oslo", 'int', -51 ),
  575. array( 'test', '<b>test</b>', false, 0, -0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'int', 0 ),
  576. array( 'test', '<b>test</b>', false, 913123, -0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'int', 913123 ),
  577. array( 'test', '<b>test</b>', true, -51, -12831e4, "2008-04-23 16:35 Europe/Oslo", 'float', -12831e4 ),
  578. array( 'test', '<b>test</b>', false, 0, 0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'float', 0.42e-9 ),
  579. array( 'test', '<b>test</b>', false, 913123, 0, "2008-04-23 16:35 Europe/Oslo", 'float', 0 ),
  580. array( 'test', '<b>test</b>', false, 913123, 0, "2008-04-23 16:35 Europe/Oslo", 'date', "2008-04-23 14:35 GMT" ),
  581. array( 'test', '<b>test</b>', false, 913123, 0, "2008-04-23 16:35 Europe/Oslo", 'date', "2008-04-23 16:35 Europe/Oslo" ),
  582. );
  583. }
  584. /**
  585. * @dataProvider datatypes
  586. */
  587. public function testDataTypes1( $string, $html, $bool, $int, $float, $date, $findField, $findValue )
  588. {
  589. $session = new ezcSearchSession( $this->backend, new ezcSearchEmbeddedManager() );
  590. $a = new DataTypeTest( 'testId', $string, $html, $bool, $int, $float, $date );
  591. $session->index( $a );
  592. $q = $session->createFindQuery( 'DataTypeTest' );
  593. $q->where( $q->eq( $findField, $findValue ) );
  594. $r = $session->find( $q );
  595. self::assertEquals( 1, $r->resultCount );
  596. self::assertEquals( $string, $r->documents['testId']->document->string );
  597. self::assertEquals( $html, $r->documents['testId']->document->html );
  598. self::assertEquals( $bool, $r->documents['testId']->document->bool );
  599. self::assertEquals( $int, $r->documents['testId']->document->int );
  600. self::assertEquals( $float, $r->documents['testId']->document->float );
  601. self::assertEquals( date_create( "$date" )->format( '\sU' ), $r->documents['testId']->document->date->format( '\sU' ) );
  602. }
  603. /**
  604. * @dataProvider datatypes
  605. */
  606. public function testDataTypes2( $string, $html, $bool, $int, $float, $date, $findField, $findValue )
  607. {
  608. $session = new ezcSearchSession( $this->backend, new ezcSearchEmbeddedManager() );
  609. $a = new DataTypeTestMulti( 'testId', $string, $html, $bool, $int, $float, $date );
  610. $session->index( $a );
  611. $q = $session->createFindQuery( 'DataTypeTestMulti' );
  612. $q->where( $q->eq( $findField, $findValue ) );
  613. $r = $session->find( $q );
  614. self::assertEquals( 1, $r->resultCount );
  615. self::assertEquals( array( $string ), $r->documents['testId']->document->string );
  616. self::assertEquals( array( $html ), $r->documents['testId']->document->html );
  617. self::assertEquals( array( $bool ), $r->documents['testId']->document->bool );
  618. self::assertEquals( array( $int ), $r->documents['testId']->document->int );
  619. self::assertEquals( array( $float ), $r->documents['testId']->document->float );
  620. self::assertEquals( $string, $r->documents['testId']->document->string[0] );
  621. self::assertEquals( $html, $r->documents['testId']->document->html[0] );
  622. self::assertEquals( $bool, $r->documents['testId']->document->bool[0] );
  623. self::assertEquals( $int, $r->documents['testId']->document->int[0] );
  624. self::assertEquals( $float, $r->documents['testId']->document->float[0] );
  625. self::assertEquals( date_create( "$date" )->format( '\sU' ), $r->documents['testId']->document->date[0]->format( '\sU' ) );
  626. }
  627. public static function datatypesMulti()
  628. {
  629. return array(
  630. array( array( 'test', 'to' ), array( '<b>test</b>', '<i>and</i>' ), true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'id', 'testId' ),
  631. array( array( 'test', 'be' ), array( '<b>test</b>', '<i>human</i>' ), false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'id', 'testId' ),
  632. array( array( 'test', 'or' ), array( '<b>test</b>', '<i>stupidity</i>' ), true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'string', 'or' ),
  633. array( array( 'test', 'or' ), array( '<b>test</b>', '<i>And</i>' ), true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'string', 'test' ),
  634. array( array( 'test', 'not' ), array( '<b>test</b>', '<i>I</i>' ), true, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'string', 'tes*' ),
  635. array( array( 'test', 'to' ), array( '<b>test</b>', '<i>am</i>' ), false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', 'test' ),
  636. array( array( 'test', 'be' ), array( '<b>test</b>', '<i>not</i>' ), false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', 'not' ),
  637. array( array( 'test', 'that' ), array( '<b>test</b>', '<i>sure</i>' ), false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', '<b>test</b>' ),
  638. array( array( 'test', 'that' ), array( '<b>test</b>', '<i>about</i>' ), false, 42, 0.42, "2008-04-23 16:35 Europe/Oslo", 'html', '<i>about</i>' ),
  639. array( array( 'test', 'is' ), array( '<b>test</b>', '<i>the</i>' ), true, -42, -12831e4, "2008-04-23 16:35 Europe/Oslo", 'bool', true ),
  640. array( array( 'test', 'the' ), array( '<b>test</b>', '<i>former</i>' ), false, -42, -0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'bool', false ),
  641. array( array( 'test', 'question' ), array( '<b>test</b>', ), true, array( 1, 1, 2, 3, 5, 8 ), -12831e4, "2008-04-23 16:35 Europe/Oslo", 'int', 5 ),
  642. array( array( 'test' ), array( '<b>test</b>', ), array( true, false ), 0, -0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'bool', true ),
  643. array( array( 'test', 'Only' ), array( '<b>test</b>', ), array( true, false ), 913123, -0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'bool', false ),
  644. array( array( 'test', 'two' ), array( '<b>test</b>', ), true, -51, array( 3.1415926 ), "2008-04-23 16:35 Europe/Oslo", 'float', 3.1415926 ),
  645. array( array( 'test', 'things' ), array( '<b>test</b>', ), false, 0, 0.42e-9, "2008-04-23 16:35 Europe/Oslo", 'float', 0.42e-9 ),
  646. array( array( 'test', 'are' ), array( '<b>test</b>', ), false, 913123, 0, array( "2008-04-23 16:35 Europe/Oslo" ), 'date', "2008-04-23 14:35 GMT" ),
  647. array( array( 'test', 'infinite' ), array( '<b>test</b>', ), false, 913123, 0, array( "2008-04-23 16:35 Europe/Oslo", "2008-04-23 15:35 BST" ), 'date', "2008-04-23 14:35 GMT" ),
  648. array( array( 'test', 'universe' ), array( '<b>test</b>', ), false, 913123, 0, "2008-04-23 16:35 Europe/Oslo", 'date', "2008-04-23 16:35 Europe/Oslo" ),
  649. );
  650. }
  651. /**
  652. * @dataProvider datatypesMulti
  653. */
  654. public function testDataTypes3( $string, $html, $bool, $int, $float, $date, $findField, $findValue )
  655. {
  656. $session = new ezcSearchSession( $this->backend, new ezcSearchEmbeddedManager() );
  657. $string = is_array( $string ) ? $string : array( $string );
  658. $html = is_array( $html ) ? $html : array( $html );
  659. $bool = is_array( $bool ) ? $bool : array( $bool );
  660. $int = is_array( $int ) ? $int : array( $int );
  661. $float = is_array( $float ) ? $float : array( $float );
  662. $date = is_array( $date ) ? $date : array( $date );
  663. $a = new DataTypeTestMulti( 'testId', $string, $html, $bool, $int, $float, $date );
  664. $session->index( $a );
  665. $q = $session->createFindQuery( 'DataTypeTestMulti' );
  666. $q->where( $q->eq( $findField, $findValue ) );
  667. $r = $session->find( $q );
  668. self::assertEquals( 1, $r->resultCount );
  669. self::assertEquals( $string, $r->documents['testId']->document->string );
  670. self::assertEquals( $html, $r->documents['testId']->document->html );
  671. self::assertEquals( $bool, $r->documents['testId']->document->bool );
  672. self::assertEquals( $int, $r->documents['testId']->document->int );
  673. self::assertEquals( $float, $r->documents['testId']->document->float );
  674. self::assertEquals( date_create( $date[0] )->format( '\sU' ), $r->documents['testId']->document->date[0]->format( '\sU' ) );
  675. }
  676. }
  677. ?>