You are here

class AddURLTest in Search API 8

Tests the "URL field" processor.

@group search_api

Hierarchy

Expanded class hierarchy of AddURLTest

See also

\Drupal\search_api\Plugin\search_api\processor\AddURL

File

tests/src/Unit/Processor/AddURLTest.php, line 20

Namespace

Drupal\Tests\search_api\Unit\Processor
View source
class AddURLTest extends UnitTestCase {
  use TestItemsTrait;

  /**
   * The processor to be tested.
   *
   * @var \Drupal\search_api\Plugin\search_api\processor\AddURL
   */
  protected $processor;

  /**
   * A search index mock for the tests.
   *
   * @var \Drupal\search_api\IndexInterface
   */
  protected $index;

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setUpMockContainer();

    // Mock the datasource of the indexer to return the mocked url object.
    $datasource = $this
      ->createMock(DatasourceInterface::class);
    $datasource
      ->expects($this
      ->any())
      ->method('getItemUrl')
      ->withAnyParameters()
      ->will($this
      ->returnValue(new TestUrl('/node/example')));

    // Create a mock for the index to return the datasource mock.

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $this->index = $this
      ->createMock(IndexInterface::class);
    $this->index
      ->expects($this
      ->any())
      ->method('getDatasource')
      ->with('entity:node')
      ->will($this
      ->returnValue($datasource));

    // Create the tested processor and set the mocked indexer.
    $this->processor = new AddURL([], 'add_url', []);
    $this->processor
      ->setIndex($index);

    /** @var \Drupal\Core\StringTranslation\TranslationInterface $translation */
    $translation = $this
      ->getStringTranslationStub();
    $this->processor
      ->setStringTranslation($translation);
  }

  /**
   * Tests whether the "URI" field is correctly filled by the processor.
   */
  public function testAddFieldValues() {

    /** @var \Drupal\node\Entity\Node $node */
    $node = $this
      ->getMockBuilder('Drupal\\node\\Entity\\Node')
      ->disableOriginalConstructor()
      ->getMock();
    $body_value = [
      'Some text value',
    ];
    $fields = [
      'search_api_url' => [
        'type' => 'string',
      ],
      'entity:node/body' => [
        'type' => 'text',
        'values' => $body_value,
      ],
    ];
    $items = $this
      ->createItems($this->index, 2, $fields, EntityAdapter::createFromEntity($node));
    foreach ($items as $item) {

      // Add a second URL field with "Generate absolute URL" enabled.
      $field = (clone $item
        ->getField('url'))
        ->setFieldIdentifier('url_1')
        ->setConfiguration([
        'absolute' => TRUE,
      ]);
      $item
        ->setField('url_1', $field);

      // Add the processor's field values to the items.
      $this->processor
        ->addFieldValues($item);
    }

    // Check the generated URLs.
    $item_1 = $items[$this->itemIds[0]];
    $this
      ->assertEquals([
      '/node/example',
    ], $item_1
      ->getField('url')
      ->getValues());
    $this
      ->assertEquals([
      'http://www.example.com/node/example',
    ], $item_1
      ->getField('url_1')
      ->getValues());

    // Check that no other fields were changed.
    $this
      ->assertEquals($body_value, $item_1
      ->getField('body')
      ->getValues());

    // Check the second item to be sure that all are processed.
    $item_2 = $items[$this->itemIds[1]];
    $this
      ->assertEquals([
      '/node/example',
    ], $item_2
      ->getField('url')
      ->getValues());
    $this
      ->assertEquals([
      'http://www.example.com/node/example',
    ], $item_2
      ->getField('url_1')
      ->getValues());
  }

  /**
   * Tests whether the properties are correctly altered.
   *
   * @see \Drupal\search_api\Plugin\search_api\processor\AddURL::alterPropertyDefinitions()
   */
  public function testAlterPropertyDefinitions() {

    // Check for added properties when no datasource is given.
    $properties = $this->processor
      ->getPropertyDefinitions(NULL);
    $this
      ->assertArrayHasKey('search_api_url', $properties);
    $this
      ->assertInstanceOf(AddURLProperty::class, $properties['search_api_url'], 'The "search_api_url" property contains a valid data definition.');
    $this
      ->assertEquals('string', $properties['search_api_url']
      ->getDataType(), 'Correct data type set in the data definition.');
    $this
      ->assertEquals('URI', $properties['search_api_url']
      ->getLabel(), 'Correct label set in the data definition.');
    $this
      ->assertEquals('A URI where the item can be accessed', $properties['search_api_url']
      ->getDescription(), 'Correct description set in the data definition.');

    // Verify that there are no properties if a datasource is given.
    $datasource = $this
      ->createMock(DatasourceInterface::class);
    $properties = $this->processor
      ->getPropertyDefinitions($datasource);
    $this
      ->assertEmpty($properties, 'Datasource-specific properties did not get changed.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddURLTest::$index protected property A search index mock for the tests.
AddURLTest::$processor protected property The processor to be tested.
AddURLTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
AddURLTest::testAddFieldValues public function Tests whether the "URI" field is correctly filled by the processor.
AddURLTest::testAlterPropertyDefinitions public function Tests whether the properties are correctly altered.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TestItemsTrait::$container protected property The class container.
TestItemsTrait::$itemIds protected property The used item IDs for test items.
TestItemsTrait::createItems public function Creates a certain number of test items.
TestItemsTrait::createSingleFieldItem public function Creates an array with a single item which has the given field.
TestItemsTrait::setUpMockContainer protected function Adds a container with several mock services commonly needed by our tests.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.