/**
  * Test the listener without setting an authorization service.
  * This should return an empty array.
  *
  * @return void
  */
 public function testFilterTranslation()
 {
     $params = new ParamBag(['fq' => ['foo:value', 'baz:"foo:value"', 'foofoo:value', "foo\\:value", 'baz:value OR foo:value', '(foo:value)']]);
     $listener = new FilterFieldConversionListener(['foo' => 'bar', 'baz' => 'boo']);
     $backend = $this->getMockBuilder('VuFindSearch\\Backend\\Solr\\Backend')->disableOriginalConstructor()->getMock();
     $event = new Event('pre', $backend, ['params' => $params]);
     $listener->onSearchPre($event);
     $fq = $params->get('fq');
     $expected = ['bar:value', 'boo:"foo:value"', 'foofoo:value', "foo\\:value", 'boo:value OR bar:value', '(bar:value)'];
     $this->assertEquals($expected, $fq);
 }
 /**
  * Test that loading a record overrides the shard settings.
  *
  * @return void
  */
 public function testAllShardsUsedForRecordRetrieval()
 {
     $params = new ParamBag(['shards' => [self::$shards['b'], self::$shards['c']]]);
     $event = new Event('pre', $this->backend, ['params' => $params, 'context' => 'retrieve']);
     $this->listener->onSearchPre($event);
     $shards = $params->get('shards');
     $this->assertEquals([implode(',', [self::$shards['a'], self::$shards['b'], self::$shards['c']])], $shards);
 }
Ejemplo n.º 3
0
 /**
  * Test the listener if no permission controller exists
  *
  * @return void
  */
 public function testOnCampusNoPermissionController()
 {
     $params = new ParamBag([]);
     $listener = new InjectOnCampusListener();
     $event = new Event('pre', $this->backend, ['params' => $params]);
     $listener->onSearchPre($event);
     $onCampus = $params->get('onCampus');
     $this->assertEquals([0 => false], $onCampus);
 }
 /**
  * Test the listener with preset fq-parameters
  * if the conditional filter is granted
  *
  * @return void
  */
 public function testConditionalFilterWithFQ()
 {
     $params = new ParamBag(['fq' => ['fulltext:Vufind', 'field2:novalue']]);
     $listener = new InjectConditionalFilterListener(self::$searchConfig);
     $mockAuth = $this->getMockBuilder('ZfcRbac\\Service\\AuthorizationService')->disableOriginalConstructor()->getMock();
     $mockAuth->expects($this->any())->method('isGranted')->with($this->equalTo('conditionalFilter.sample'))->will($this->returnValue(true));
     $listener->setAuthorizationService($mockAuth);
     $event = new Event('pre', $this->backend, ['params' => $params]);
     $listener->onSearchPre($event);
     $fq = $params->get('fq');
     $this->assertEquals([0 => 'fulltext:Vufind', 1 => 'field2:novalue', 2 => 'institution:"MyInst"'], $fq);
 }
Ejemplo n.º 5
0
 /**
  * Retrieve a single document.
  *
  * @param string   $id     Document identifier
  * @param ParamBag $params Search backend parameters
  *
  * @return \VuFindSearch\Response\RecordCollectionInterface
  */
 public function retrieve($id, ParamBag $params = null)
 {
     try {
         $authenticationToken = $this->getAuthenticationToken();
         // check to see if the profile is overriden
         $overrideProfile = null !== $params ? $params->get('profile') : null;
         if (isset($overrideProfile)) {
             $this->profile = $overrideProfile;
         }
         $sessionToken = $this->getSessionToken();
         $parts = explode(',', $id, 2);
         if (!isset($parts[1])) {
             throw new BackendException('Retrieval id is not in the correct format.');
         }
         list($dbId, $an) = $parts;
         $hlTerms = null != $params ? $params->get('highlightterms') : null;
         $response = $this->client->retrieve($an, $dbId, $authenticationToken, $sessionToken, $hlTerms);
     } catch (\EbscoEdsApiException $e) {
         // if the auth or session token was invalid, try once more
         switch ($e->getApiErrorCode()) {
             case 104:
             case 108:
             case 109:
                 try {
                     // For error 104, retry auth token; for 108/9, retry sess token:
                     if ($e->getApiErrorCode() == 104) {
                         $authenticationToken = $this->getAuthenticationToken(true);
                     } else {
                         $sessionToken = $this->getSessionToken(true);
                     }
                     $response = $this->client->retrieve($an, $dbId, $authenticationToken, $sessionToken, $hlTerms);
                 } catch (Exception $e) {
                     throw new BackendException($e->getMessage(), $e->getCode(), $e);
                 }
                 break;
             default:
                 throw $e;
         }
     }
     $collection = $this->createRecordCollection(['Records' => $response]);
     $this->injectSourceIdentifier($collection);
     return $collection;
 }
Ejemplo n.º 6
0
 /**
  * Retrieve a batch of documents.
  *
  * @param array    $ids    Array of document identifiers
  * @param ParamBag $params Search backend parameters
  *
  * @return RecordCollectionInterface
  */
 public function retrieveBatch($ids, ParamBag $params = null)
 {
     $onCampus = null !== $params ? $params->get('onCampus') : [false];
     $onCampus = $onCampus ? $onCampus[0] : false;
     // Load 100 records at a time; this is a good number to avoid memory
     // problems while still covering a lot of ground.
     $pageSize = 100;
     // Retrieve records a page at a time:
     $results = false;
     while (count($ids) > 0) {
         $currentPage = array_splice($ids, 0, $pageSize, []);
         try {
             $response = $this->connector->getRecords($currentPage, $this->connector->getInstitutionCode(), $onCampus);
         } catch (\Exception $e) {
             throw new BackendException($e->getMessage(), $e->getCode(), $e);
         }
         $next = $this->createRecordCollection($response);
         if (!$results) {
             $results = $next;
         } else {
             foreach ($next->getRecords() as $record) {
                 $results->add($record);
             }
         }
     }
     $this->injectSourceIdentifier($results);
     return $results;
 }
Ejemplo n.º 7
0
 /**
  * Retrieve a single document.
  *
  * @param string   $id     Document identifier
  * @param ParamBag $params Search backend parameters
  *
  * @return RecordCollectionInterface
  */
 public function retrieve($id, ParamBag $params = null)
 {
     $onCampus = null !== $params ? $params->get('onCampus') : [false];
     $onCampus = $onCampus ? $onCampus[0] : false;
     try {
         $response = $this->connector->getRecord($id, $this->connector->getInstitutionCode(), $onCampus);
     } catch (\Exception $e) {
         throw new BackendException($e->getMessage(), $e->getCode(), $e);
     }
     $collection = $this->createRecordCollection($response);
     $this->injectSourceIdentifier($collection);
     return $collection;
 }
Ejemplo n.º 8
0
 /**
  * Inject response writer and named list implementation into parameters.
  *
  * @param ParamBag $params Parameters
  *
  * @return void
  *
  * @throws InvalidArgumentException Response writer and named list
  * implementation already set to an incompatible type.
  */
 protected function injectResponseWriter(ParamBag $params)
 {
     if (array_diff($params->get('wt') ?: [], ['json'])) {
         throw new InvalidArgumentException(sprintf('Invalid response writer type: %s', implode(', ', $params->get('wt'))));
     }
     if (array_diff($params->get('json.nl') ?: [], ['arrarr'])) {
         throw new InvalidArgumentException(sprintf('Invalid named list implementation type: %s', implode(', ', $params->get('json.nl'))));
     }
     $params->set('wt', ['json']);
     $params->set('json.nl', ['arrarr']);
 }
Ejemplo n.º 9
0
 /**
  * Return SOLR search parameters based on a record Id and params.
  *
  * @param string $id Record Id
  *
  * @return ParamBag
  */
 public function build($id)
 {
     $params = new ParamBag();
     if ($this->useHandler) {
         $mltParams = $this->handlerParams ? $this->handlerParams : 'qf=title,title_short,callnumber-label,topic,language,author,' . 'publishDate mintf=1 mindf=1';
         $params->set('q', sprintf('{!mlt %s}%s', $mltParams, $id));
     } else {
         $params->set('q', sprintf('%s:"%s"', $this->uniqueKey, addcslashes($id, '"')));
         $params->set('qt', 'morelikethis');
     }
     if (null === $params->get('rows')) {
         $params->set('rows', $this->count);
     }
     return $params;
 }