You are here

class LinkTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Unit/JsonApiResource/LinkTest.php \Drupal\Tests\jsonapi\Unit\JsonApiResource\LinkTest

@coversDefaultClass \Drupal\jsonapi\JsonApiResource\Link @group jsonapi

@internal

Hierarchy

Expanded class hierarchy of LinkTest

File

core/modules/jsonapi/tests/src/Unit/JsonApiResource/LinkTest.php, line 19

Namespace

Drupal\Tests\jsonapi\Unit\JsonApiResource
View source
class LinkTest extends UnitTestCase {

  /**
   * @covers ::compare
   * @dataProvider linkComparisonProvider
   */
  public function testLinkComparison(Link $a, Link $b, $expected) {
    $actual = Link::compare($a, $b);
    $this
      ->assertSame($expected, $actual === 0);
  }

  /**
   * Provides test data for link comparison.
   */
  public function linkComparisonProvider() {
    $this
      ->mockUrlAssembler();
    return [
      'same href and same link relation type' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        TRUE,
      ],
      'different href and same link relation type' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/bar'), 'self'),
        FALSE,
      ],
      'same href and different link relation type' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'related'),
        FALSE,
      ],
      'same href and same link relation type and empty target attributes' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', []),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', []),
        TRUE,
      ],
      'same href and same link relation type and same target attributes' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', [
          'anchor' => 'https://jsonapi.org',
        ]),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', [
          'anchor' => 'https://jsonapi.org',
        ]),
        TRUE,
      ],
      // These links are not considered equivalent because it would while the
      // `href` remains the same, the anchor changes the context of the link.
      'same href and same link relation type and different target attributes' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/boy'), 'self', [
          'title' => 'sue',
        ]),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/boy'), 'self', [
          'anchor' => '/sob',
          'title' => 'pa',
        ]),
        FALSE,
      ],
      'same href and same link relation type and same nested target attributes' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', [
          'data' => [
            'foo' => 'bar',
          ],
        ]),
        new Link(new cacheablemetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', [
          'data' => [
            'foo' => 'bar',
          ],
        ]),
        TRUE,
      ],
      'same href and same link relation type and different nested target attributes' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', [
          'data' => [
            'foo' => 'bar',
          ],
        ]),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self', [
          'data' => [
            'foo' => 'baz',
          ],
        ]),
        FALSE,
      ],
      // These links are not considered equivalent because it would be unclear
      // which title corresponds to which link relation type.
      'same href and different link relation types and different target attributes' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/boy'), 'self', [
          'title' => 'A boy named Sue',
        ]),
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/boy'), 'edit', [
          'title' => 'Change name to Bill or George',
        ]),
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::merge
   * @dataProvider linkMergeProvider
   */
  public function testLinkMerge(Link $a, Link $b, $expected) {
    if ($expected instanceof Link) {
      $this
        ->assertSame($expected
        ->getCacheTags(), Link::merge($a, $b)
        ->getCacheTags());
    }
    else {
      $this
        ->expectExceptionObject($expected);
      Link::merge($a, $b);
    }
  }

  /**
   * Provides test data for link merging.
   */
  public function linkMergeProvider() {
    $this
      ->mockUrlAssembler();
    return [
      'same everything' => [
        new Link((new CacheableMetadata())
          ->addCacheTags([
          'foo',
        ]), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link((new CacheableMetadata())
          ->addCacheTags([
          'foo',
        ]), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link((new CacheableMetadata())
          ->addCacheTags([
          'foo',
        ]), Url::fromUri('https://jsonapi.org/foo'), 'self'),
      ],
      'different cache tags' => [
        new Link((new CacheableMetadata())
          ->addCacheTags([
          'foo',
        ]), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link((new CacheableMetadata())
          ->addCacheTags([
          'bar',
        ]), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        new Link((new CacheableMetadata())
          ->addCacheTags([
          'foo',
          'bar',
        ]), Url::fromUri('https://jsonapi.org/foo'), 'self'),
      ],
    ];
  }

  /**
   * @covers ::getLinkRelationType
   */
  public function testGetLinkRelationType() {
    $this
      ->mockUrlAssembler();
    $link = new Link((new CacheableMetadata())
      ->addCacheTags([
      'foo',
    ]), Url::fromUri('https://jsonapi.org/foo'), 'self');
    $this
      ->assertSame('self', $link
      ->getLinkRelationType());
  }

  /**
   * @group legacy
   * @expectedDeprecation Constructing a Drupal\jsonapi\JsonApiResource\Link with an array of link relation types is deprecated in drupal:8.8.0 and will throw a fatal error in drupal:9.0.0. Pass a single string instead. See https://www.drupal.org/node/3087821.
   * @expectedDeprecation Drupal\jsonapi\JsonApiResource\Link::getLinkRelationTypes() is deprecated in drupal:8.8.0 and will be removed in drupal:9.0.0. Use getLinkRelationType() instead. See https://www.drupal.org/node/3087821.
   * @covers ::__construct
   * @covers ::getLinkRelationTypes
   */
  public function testLinkDeprecations() {
    $this
      ->mockUrlAssembler();
    $link = new Link((new CacheableMetadata())
      ->addCacheTags([
      'foo',
    ]), Url::fromUri('https://jsonapi.org/foo'), [
      'self',
      'foo',
    ]);
    $this
      ->assertSame([
      'self',
      'foo',
    ], $link
      ->getLinkRelationTypes());
    $this
      ->assertSame('self', $link
      ->getLinkRelationType());
    $link = new Link((new CacheableMetadata())
      ->addCacheTags([
      'foo',
    ]), Url::fromUri('https://jsonapi.org/foo'), 'self');
    $this
      ->assertSame([
      'self',
    ], $link
      ->getLinkRelationTypes());
  }

  /**
   * Mocks the unrouted URL assembler.
   */
  protected function mockUrlAssembler() {
    $url_assembler = $this
      ->getMockBuilder(UnroutedUrlAssemblerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $url_assembler
      ->method('assemble')
      ->will($this
      ->returnCallback(function ($uri) {
      return (new GeneratedUrl())
        ->setGeneratedUrl($uri);
    }));
    $container = new ContainerBuilder();
    $container
      ->set('unrouted_url_assembler', $url_assembler);
    \Drupal::setContainer($container);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkTest::linkComparisonProvider public function Provides test data for link comparison.
LinkTest::linkMergeProvider public function Provides test data for link merging.
LinkTest::mockUrlAssembler protected function Mocks the unrouted URL assembler.
LinkTest::testGetLinkRelationType public function @covers ::getLinkRelationType
LinkTest::testLinkComparison public function @covers ::compare @dataProvider linkComparisonProvider
LinkTest::testLinkDeprecations public function @group legacy @expectedDeprecation Constructing a Drupal\jsonapi\JsonApiResource\Link with an array of link relation types is deprecated in drupal:8.8.0 and will throw a fatal error in drupal:9.0.0. Pass a single string instead. See…
LinkTest::testLinkMerge public function @covers ::merge @dataProvider linkMergeProvider
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.
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.
UnitTestCase::setUp protected function 340