Open Preprint Systems  3.3.0
ArticleSearchTest.php
1 <?php
2 
17 require_mock_env('env1');
18 
19 import('lib.pkp.tests.PKPTestCase');
20 import('lib.pkp.classes.core.ArrayItemIterator');
21 import('classes.search.ArticleSearch');
22 import('lib.pkp.classes.core.PKPRouter');
23 
24 define('SUBMISSION_SEARCH_TEST_DEFAULT_ARTICLE', 1);
25 define('SUBMISSION_SEARCH_TEST_ARTICLE_FROM_PLUGIN', 2);
26 
29  private $_retrieveResultsParams;
30 
31  //
32  // Implementing protected template methods from PKPTestCase
33  //
37  protected function getMockedDAOs() {
38  $mockedDaos = parent::getMockedDAOs();
39  $mockedDaos += array(
40  'ArticleSearchDAO', 'SubmissionDAO',
41  'JournalDAO', 'SectionDAO'
42  );
43  return $mockedDaos;
44  }
45 
49  protected function setUp() : void {
50  parent::setUp();
52 
53  // Prepare the mock environment for this test.
54  $this->registerMockArticleSearchDAO();
55  $this->registerMockSubmissionDAO();
56  $this->registerMockJournalDAO();
57  $this->registerMockSectionDAO();
58 
59  $request = Application::get()->getRequest();
60  if (is_null($request->getRouter())) {
61  $router = new PKPRouter();
62  $request->setRouter($router);
63  }
64  }
65 
69  protected function tearDown() : void {
71  parent::tearDown();
72  }
73 
74 
75  //
76  // Unit tests
77  //
81  public function testRetrieveResults() {
82  $this->markTestSkipped(); // Temporarily disabled!
83 
84  // Make sure that no hook is being called.
85  HookRegistry::clear('SubmissionSearch::retrieveResults');
86 
87  // Test a simple search with a mock database back-end.
88  $journal = new Journal();
89  $keywords = array(null => 'test');
90  $articleSearch = new ArticleSearch();
91  $error = '';
92  $request = Application::get()->getRequest();
93  $searchResult = $articleSearch->retrieveResults($request, $journal, $keywords, $error);
94 
95  // Test whether the result from the mocked DAOs is being returned.
96  self::assertInstanceOf('ItemIterator', $searchResult);
97  $firstResult = $searchResult->next();
98  self::assertArrayHasKey('article', $firstResult);
99  self::assertEquals(SUBMISSION_SEARCH_TEST_DEFAULT_ARTICLE, $firstResult['article']->getId());
100  self::assertEquals('', $error);
101 
102  $this->registerMockArticleSearchDAO(); // This is necessary to instantiate a fresh iterator.
103  $keywords = array(null => 'test');
104  $searchResult = $articleSearch->retrieveResults($request, $journal, $keywords, $error);
105  self::assertTrue($searchResult->eof());
106  }
107 
111  public function testRetrieveResultsViaPluginHook() {
112  $this->markTestSkipped(); // Temporarily disabled!
113 
114  // Diverting a search to the search plugin hook.
115  HookRegistry::register('SubmissionSearch::retrieveResults', array($this, 'callbackRetrieveResults'));
116 
117  $testCases = array(
118  array (null => 'query'), // Simple Search - "All"
119  array ('1' => 'author'), // Simple Search - "Authors"
120  array ('2' => 'title'), // Simple Search - "Title"
121  array (
122  null => 'query',
123  1 => 'author',
124  2 => 'title'
125  ), // Advanced Search
126  );
127 
128  $testFromDate = date('Y-m-d H:i:s', strtotime('2011-03-15 00:00:00'));
129  $testToDate = date('Y-m-d H:i:s', strtotime('2012-03-15 18:30:00'));
130  $error = '';
131 
132  $request = Application::get()->getRequest();
133 
134  foreach($testCases as $testCase) {
135  // Test a simple search with the simulated callback.
136  $journal = new Journal();
137  $keywords = $testCase;
138  $articleSearch = new ArticleSearch();
139  $searchResult = $articleSearch->retrieveResults($request, $journal, $keywords, $error, $testFromDate, $testToDate);
140 
141  // Check the parameters passed into the callback.
142  $expectedPage = 1;
143  $expectedItemsPerPage = 20;
144  $expectedTotalResults = 3;
145  $expectedError = '';
146  $expectedParams = array(
147  $journal, $testCase, $testFromDate, $testToDate,
148  $expectedPage, $expectedItemsPerPage, $expectedTotalResults,
149  $expectedError
150  );
151  self::assertEquals($expectedParams, $this->_retrieveResultsParams);
152 
153  // Test and clear the call history of the hook registry.
154  $calledHooks = HookRegistry::getCalledHooks();
155  self::assertEquals('SubmissionSearch::retrieveResults', $calledHooks[0][0]);
157 
158  // Test whether the result from the hook is being returned.
159  self::assertInstanceOf('VirtualArrayIterator', $searchResult);
160 
161  // Test the total count.
162  self::assertEquals(3, $searchResult->getCount());
163 
164  // Test the search result.
165  $firstResult = $searchResult->next();
166  self::assertArrayHasKey('article', $firstResult);
167  self::assertEquals(SUBMISSION_SEARCH_TEST_ARTICLE_FROM_PLUGIN, $firstResult['article']->getId());
168  self::assertEquals('', $error);
169  }
170 
171  // Remove the test hook.
172  HookRegistry::clear('SubmissionSearch::retrieveResults');
173  }
174 
175 
176  //
177  // Public callback methods
178  //
183  public function callbackRetrieveResults($hook, $params) {
184  // Save the test parameters
185  $this->_retrieveResultsParams = $params;
186 
187  // Test returning count by-ref.
188  $totalCount =& $params[6];
189  $totalCount = 3;
190 
191  // Mock a result set and return it.
192  $results = array(
193  3 => SUBMISSION_SEARCH_TEST_ARTICLE_FROM_PLUGIN
194  );
195  return $results;
196  }
197 
204  public function callbackGetArticle($articleId, $journalId = null, $useCache = false) {
205  // Create an article instance with the correct id.
206  $article = new Submission();
207  $article->setId($articleId);
208  return $article;
209  }
210 
211 
212  //
213  // Private helper methods
214  //
219  private function registerMockArticleSearchDAO() {
220  // Mock an ArticleSearchDAO.
221  $articleSearchDAO = $this->getMockBuilder(ArticleSearchDAO::class)
222  ->setMethods(array('getPhraseResults'))
223  ->getMock();
224 
225  // Mock a result set.
226  $searchResult = array(
227  SUBMISSION_SEARCH_TEST_DEFAULT_ARTICLE => array(
228  'count' => 3,
229  'journal_id' => 2,
230  'publicationDate' => '2013-05-01 20:30:00'
231  )
232  );
233 
234  // Mock the getPhraseResults() method.
235  $articleSearchDAO->expects($this->any())
236  ->method('getPhraseResults')
237  ->will($this->returnValue($searchResult));
238 
239  // Register the mock DAO.
240  DAORegistry::registerDAO('ArticleSearchDAO', $articleSearchDAO);
241  }
242 
247  private function registerMockSubmissionDAO() {
248  // Mock an SubmissionDAO.
249  $submissionDao = $this->getMockBuilder(SubmissionDAO::class)
250  ->setMethods(array('getArticle'))
251  ->getMock();
252 
253  // Mock an article.
254  $article = new Submission();
255 
256  // Mock the getArticle() method.
257  $submissionDao->expects($this->any())
258  ->method('getArticle')
259  ->will($this->returnCallback(array($this, 'callbackGetArticle')));
260 
261  // Register the mock DAO.
262  DAORegistry::registerDAO('SubmissionDAO', $submissionDao);
263  }
264 
269  private function registerMockJournalDAO() {
270  // Mock a JournalDAO.
271  $journalDAO = $this->getMockBuilder(JournalDAO::class)
272  ->setMethods(array('getById'))
273  ->getMock();
274 
275  // Mock a journal.
276  $journal = new Journal();
277 
278  // Mock the getById() method.
279  $journalDAO->expects($this->any())
280  ->method('getById')
281  ->will($this->returnValue($journal));
282 
283  // Register the mock DAO.
284  DAORegistry::registerDAO('JournalDAO', $journalDAO);
285  }
286 
291  private function registerMockSectionDAO() {
292  // Mock a SectionDAO.
293  $sectionDAO = $this->getMockBuilder(SectionDAO::class)
294  ->setMethods(array('getSection'))
295  ->getMock();
296 
297  // Mock a section.
298  $section = new Section();
299 
300  // Mock the getSection() method.
301  $sectionDAO->expects($this->any())
302  ->method('getSection')
303  ->will($this->returnValue($section));
304 
305  // Register the mock DAO.
306  DAORegistry::registerDAO('SectionDAO', $sectionDAO);
307  }
308 }
309 
ArticleSearchTest\callbackGetArticle
callbackGetArticle($articleId, $journalId=null, $useCache=false)
Definition: ArticleSearchTest.php:207
HookRegistry\resetCalledHooks
static resetCalledHooks($leaveAlive=false)
Definition: HookRegistry.inc.php:143
Submission
Article class.
Definition: Submission.inc.php:33
ArticleSearchTest\callbackRetrieveResults
callbackRetrieveResults($hook, $params)
Definition: ArticleSearchTest.php:186
HookRegistry\rememberCalledHooks
static rememberCalledHooks($askOnly=false, $updateTo=true)
Definition: HookRegistry.inc.php:128
HookRegistry\clear
static clear($hookName)
Definition: HookRegistry.inc.php:58
ArticleSearchTest\getMockedDAOs
getMockedDAOs()
Definition: ArticleSearchTest.php:40
ArticleSearchTest\tearDown
tearDown()
Definition: ArticleSearchTest.php:72
PKPTestCase
Class that implements functionality common to all PKP unit test cases.
Definition: PKPTestCase.inc.php:27
HookRegistry\getCalledHooks
static & getCalledHooks()
Definition: HookRegistry.inc.php:153
DAORegistry\registerDAO
static registerDAO($name, $dao)
Definition: DAORegistry.inc.php:40
ArticleSearchTest\setUp
setUp()
Definition: ArticleSearchTest.php:52
PKPRouter
Basic router class that has functionality common to all routers.
Definition: PKPRouter.inc.php:57
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
Journal
Describes basic journal properties.
Definition: Journal.inc.php:29
ArticleSearchTest\testRetrieveResultsViaPluginHook
testRetrieveResultsViaPluginHook()
Definition: ArticleSearchTest.php:114
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
ArticleSearchTest
Test class for the ArticleSearch class.
Definition: ArticleSearchTest.php:27
Section
Describes basic section properties.
Definition: Section.inc.php:19
ArticleSearch
Class for retrieving article search results.
Definition: ArticleSearch.inc.php:20
ArticleSearchTest\testRetrieveResults
testRetrieveResults()
Definition: ArticleSearchTest.php:84