You are here

class RelationshipNormalizerValueTest in JSON:API 8

@coversDefaultClass \Drupal\jsonapi\Normalizer\Value\RelationshipNormalizerValue @group jsonapi

@internal

Hierarchy

Expanded class hierarchy of RelationshipNormalizerValueTest

File

tests/src/Unit/Normalizer/Value/RelationshipNormalizerValueTest.php, line 22

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer\Value
View source
class RelationshipNormalizerValueTest extends UnitTestCase {

  /**
   * The cache contexts manager.
   *
   * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheContextsManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->cacheContextsManager = $this
      ->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->cacheContextsManager
      ->method('assertValidTokens')
      ->willReturn(TRUE);
    $container = new ContainerBuilder();
    $container
      ->set('cache_contexts_manager', $this->cacheContextsManager);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::rasterizeValue
   * @dataProvider rasterizeValueProvider
   */
  public function testRasterizeValue($values, $cardinality, $expected, CacheableMetadata $expected_cacheability) {
    $link_manager = $this
      ->prophesize(LinkManager::class);
    $link_manager
      ->getEntityLink(Argument::any(), Argument::any(), Argument::type('array'), Argument::type('string'))
      ->willReturn('dummy_entity_link');
    $resource_type = new ResourceType($this
      ->randomMachineName(), $this
      ->randomMachineName(), NULL);
    $resource_type
      ->setRelatableResourceTypes([
      'ipsum' => [
        $resource_type,
      ],
    ]);
    $object = new RelationshipNormalizerValue(AccessResult::allowed()
      ->cachePerUser()
      ->addCacheTags([
      'relationship:foo',
    ]), $values, $cardinality, [
      'link_manager' => $link_manager
        ->reveal(),
      'host_entity_id' => 'lorem',
      'resource_type' => $resource_type,
      'field_name' => 'ipsum',
    ]);
    $this
      ->assertEquals($expected, $object
      ->rasterizeValue());
    $this
      ->assertSame($expected_cacheability
      ->getCacheContexts(), $object
      ->getCacheContexts());
    $this
      ->assertSame($expected_cacheability
      ->getCacheTags(), $object
      ->getCacheTags());
    $this
      ->assertSame($expected_cacheability
      ->getCacheMaxAge(), $object
      ->getCacheMaxAge());
  }

  /**
   * Data provider fortestRasterizeValue.
   */
  public function rasterizeValueProvider() {
    $uid_raw = 1;
    $uid1 = $this
      ->prophesize(RelationshipItemNormalizerValue::class);
    $uid1
      ->rasterizeValue()
      ->willReturn([
      'type' => 'user',
      'id' => $uid_raw++,
    ]);
    $uid1
      ->getInclude()
      ->willReturn(NULL);
    $uid1
      ->getCacheContexts()
      ->willReturn([
      'ccfoo',
    ]);
    $uid1
      ->getCacheTags()
      ->willReturn([
      'ctfoo',
    ]);
    $uid1
      ->getCacheMaxAge()
      ->willReturn(15);
    $uid2 = $this
      ->prophesize(RelationshipItemNormalizerValue::class);
    $uid2
      ->rasterizeValue()
      ->willReturn([
      'type' => 'user',
      'id' => $uid_raw,
    ]);
    $uid2
      ->getInclude()
      ->willReturn(NULL);
    $uid2
      ->getCacheContexts()
      ->willReturn([
      'ccbar',
    ]);
    $uid2
      ->getCacheTags()
      ->willReturn([
      'ctbar',
    ]);
    $uid2
      ->getCacheMaxAge()
      ->willReturn(10);
    $img_id = $this
      ->randomMachineName();
    $img1 = $this
      ->prophesize(RelationshipItemNormalizerValue::class);
    $img1
      ->rasterizeValue()
      ->willReturn([
      'type' => 'file--file',
      'id' => $img_id,
      'meta' => [
        'alt' => 'Cute llama',
        'title' => 'My spirit animal',
      ],
    ]);
    $img1
      ->getInclude()
      ->willReturn(NULL);
    $img1
      ->getCacheContexts()
      ->willReturn([
      'ccimg1',
    ]);
    $img1
      ->getCacheTags()
      ->willReturn([
      'ctimg1',
    ]);
    $img1
      ->getCacheMaxAge()
      ->willReturn(100);
    $img2 = $this
      ->prophesize(RelationshipItemNormalizerValue::class);
    $img2
      ->rasterizeValue()
      ->willReturn([
      'type' => 'file--file',
      'id' => $img_id,
      'meta' => [
        'alt' => 'Adorable llama',
        'title' => 'My spirit animal ๐Ÿ˜',
      ],
    ]);
    $img2
      ->getInclude()
      ->willReturn(NULL);
    $img2
      ->getCacheContexts()
      ->willReturn([
      'ccimg2',
    ]);
    $img2
      ->getCacheTags()
      ->willReturn([
      'ctimg2',
    ]);
    $img2
      ->getCacheMaxAge()
      ->willReturn(50);
    $links = [
      'self' => 'dummy_entity_link',
      'related' => 'dummy_entity_link',
    ];
    return [
      'single cardinality' => [
        [
          $uid1
            ->reveal(),
        ],
        1,
        [
          'data' => [
            'type' => 'user',
            'id' => 1,
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccfoo',
          'user',
        ])
          ->setCacheTags([
          'ctfoo',
          'relationship:foo',
        ])
          ->setCacheMaxAge(15),
      ],
      'multiple cardinality' => [
        [
          $uid1
            ->reveal(),
          $uid2
            ->reveal(),
        ],
        2,
        [
          'data' => [
            [
              'type' => 'user',
              'id' => 1,
            ],
            [
              'type' => 'user',
              'id' => 2,
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccbar',
          'ccfoo',
          'user',
        ])
          ->setCacheTags([
          'ctbar',
          'ctfoo',
          'relationship:foo',
        ])
          ->setCacheMaxAge(10),
      ],
      'multiple cardinality, all same values' => [
        [
          $uid1
            ->reveal(),
          $uid1
            ->reveal(),
        ],
        2,
        [
          'data' => [
            [
              'type' => 'user',
              'id' => 1,
              'meta' => [
                'arity' => 0,
              ],
            ],
            [
              'type' => 'user',
              'id' => 1,
              'meta' => [
                'arity' => 1,
              ],
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccfoo',
          'user',
        ])
          ->setCacheTags([
          'ctfoo',
          'relationship:foo',
        ])
          ->setCacheMaxAge(15),
      ],
      'multiple cardinality, some same values' => [
        [
          $uid1
            ->reveal(),
          $uid2
            ->reveal(),
          $uid1
            ->reveal(),
        ],
        2,
        [
          'data' => [
            [
              'type' => 'user',
              'id' => 1,
              'meta' => [
                'arity' => 0,
              ],
            ],
            [
              'type' => 'user',
              'id' => 2,
            ],
            [
              'type' => 'user',
              'id' => 1,
              'meta' => [
                'arity' => 1,
              ],
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccbar',
          'ccfoo',
          'user',
        ])
          ->setCacheTags([
          'ctbar',
          'ctfoo',
          'relationship:foo',
        ])
          ->setCacheMaxAge(10),
      ],
      'single cardinality, with meta' => [
        [
          $img1
            ->reveal(),
        ],
        1,
        [
          'data' => [
            'type' => 'file--file',
            'id' => $img_id,
            'meta' => [
              'alt' => 'Cute llama',
              'title' => 'My spirit animal',
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccimg1',
          'user',
        ])
          ->setCacheTags([
          'ctimg1',
          'relationship:foo',
        ])
          ->setCacheMaxAge(100),
      ],
      'multiple cardinality, all same values, with meta' => [
        [
          $img1
            ->reveal(),
          $img1
            ->reveal(),
        ],
        2,
        [
          'data' => [
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Cute llama',
                'title' => 'My spirit animal',
                'arity' => 0,
              ],
            ],
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Cute llama',
                'title' => 'My spirit animal',
                'arity' => 1,
              ],
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccimg1',
          'user',
        ])
          ->setCacheTags([
          'ctimg1',
          'relationship:foo',
        ])
          ->setCacheMaxAge(100),
      ],
      'multiple cardinality, some same values with same values but different meta' => [
        [
          $img1
            ->reveal(),
          $img1
            ->reveal(),
          $img2
            ->reveal(),
        ],
        2,
        [
          'data' => [
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Cute llama',
                'title' => 'My spirit animal',
                'arity' => 0,
              ],
            ],
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Cute llama',
                'title' => 'My spirit animal',
                'arity' => 1,
              ],
            ],
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Adorable llama',
                'title' => 'My spirit animal ๐Ÿ˜',
                'arity' => 2,
              ],
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccimg1',
          'ccimg2',
          'user',
        ])
          ->setCacheTags([
          'ctimg1',
          'ctimg2',
          'relationship:foo',
        ])
          ->setCacheMaxAge(50),
      ],
      'all the edge cases!' => [
        [
          $img1
            ->reveal(),
          $img1
            ->reveal(),
          $img2
            ->reveal(),
          $uid1
            ->reveal(),
          $uid2
            ->reveal(),
          $uid1
            ->reveal(),
        ],
        10,
        [
          'data' => [
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Cute llama',
                'title' => 'My spirit animal',
                'arity' => 0,
              ],
            ],
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Cute llama',
                'title' => 'My spirit animal',
                'arity' => 1,
              ],
            ],
            [
              'type' => 'file--file',
              'id' => $img_id,
              'meta' => [
                'alt' => 'Adorable llama',
                'title' => 'My spirit animal ๐Ÿ˜',
                'arity' => 2,
              ],
            ],
            [
              'type' => 'user',
              'id' => 1,
              'meta' => [
                'arity' => 0,
              ],
            ],
            [
              'type' => 'user',
              'id' => 2,
            ],
            [
              'type' => 'user',
              'id' => 1,
              'meta' => [
                'arity' => 1,
              ],
            ],
          ],
          'links' => $links,
        ],
        (new CacheableMetadata())
          ->setCacheContexts([
          'ccbar',
          'ccfoo',
          'ccimg1',
          'ccimg2',
          'user',
        ])
          ->setCacheTags([
          'ctbar',
          'ctfoo',
          'ctimg1',
          'ctimg2',
          'relationship:foo',
        ])
          ->setCacheMaxAge(10),
      ],
    ];
  }

  /**
   * @covers ::rasterizeValue
   */
  public function testRasterizeValueFails() {
    $uid1 = $this
      ->prophesize(FieldItemNormalizerValue::class);
    $uid1
      ->rasterizeValue()
      ->willReturn(1);
    $link_manager = $this
      ->prophesize(LinkManager::class);
    $link_manager
      ->getEntityLink(Argument::any(), Argument::any(), Argument::type('array'), Argument::type('string'))
      ->willReturn('dummy_entity_link');
    $this
      ->setExpectedException(\RuntimeException::class, 'Unexpected normalizer item value for this Drupal\\jsonapi\\Normalizer\\Value\\RelationshipNormalizerValue.');
    new RelationshipNormalizerValue(AccessResult::allowed(), [
      $uid1
        ->reveal(),
    ], 1, [
      'link_manager' => $link_manager
        ->reveal(),
      'host_entity_id' => 'lorem',
      'resource_type' => new ResourceType($this
        ->randomMachineName(), $this
        ->randomMachineName(), NULL),
      'field_name' => 'ipsum',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
RelationshipNormalizerValueTest::$cacheContextsManager protected property The cache contexts manager.
RelationshipNormalizerValueTest::rasterizeValueProvider public function Data provider fortestRasterizeValue.
RelationshipNormalizerValueTest::setUp protected function Overrides UnitTestCase::setUp
RelationshipNormalizerValueTest::testRasterizeValue public function @covers ::rasterizeValue @dataProvider rasterizeValueProvider
RelationshipNormalizerValueTest::testRasterizeValueFails public function @covers ::rasterizeValue
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.