Chris@0: installEntitySchema('node'); Chris@0: Chris@0: NodeType::create([ Chris@0: 'type' => 'page', Chris@0: ])->save(); Chris@0: FieldStorageConfig::create([ Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'entity_reference', Chris@0: 'field_name' => 'field_ref', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => 'field_ref', Chris@0: ])->save(); Chris@0: Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getTypeUri Chris@0: * @dataProvider providerTestGetTypeUri Chris@0: */ Chris@0: public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) { Chris@0: $hal_settings = \Drupal::configFactory()->getEditable('hal.settings'); Chris@0: Chris@0: if ($link_domain === NULL) { Chris@0: $hal_settings->clear('link_domain'); Chris@0: } Chris@0: else { Chris@0: $hal_settings->set('link_domain', $link_domain)->save(TRUE); Chris@0: } Chris@0: Chris@0: /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */ Chris@0: $type_manager = \Drupal::service('hal.link_manager.type'); Chris@0: Chris@0: $link = $type_manager->getTypeUri($entity_type, $bundle, $context); Chris@0: $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('', [], ['absolute' => TRUE])->toString(), $expected_return)); Chris@0: $this->assertEquals($context, $expected_context); Chris@0: } Chris@0: Chris@0: public function providerTestGetTypeUri() { Chris@0: $base_test_case = [ Chris@0: 'link_domain' => NULL, Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'site URL' => $base_test_case + [ Chris@0: 'context' => [], Chris@0: 'link_domain' => NULL, Chris@0: 'expected return' => 'BASE_URL/rest/type/node/page', Chris@0: 'expected context' => [], Chris@0: ], Chris@0: // Test hook_hal_type_uri_alter(). Chris@0: 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['hal_test' => TRUE], Chris@0: 'expected return' => 'hal_test_type', Chris@0: 'expected context' => ['hal_test' => TRUE], Chris@0: ], Chris@0: // Test hook_rest_type_uri_alter() — for backwards compatibility. Chris@0: 'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['rest_test' => TRUE], Chris@0: 'expected return' => 'rest_test_type', Chris@0: 'expected context' => ['rest_test' => TRUE], Chris@0: ], Chris@0: 'configured URL' => [ Chris@0: 'link_domain' => 'http://llamas-rock.com/for-real/', Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'context' => [], Chris@0: 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page', Chris@0: 'expected context' => [], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getRelationUri Chris@0: * @dataProvider providerTestGetRelationUri Chris@0: */ Chris@0: public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) { Chris@0: $hal_settings = \Drupal::configFactory()->getEditable('hal.settings'); Chris@0: Chris@0: if ($link_domain === NULL) { Chris@0: $hal_settings->clear('link_domain'); Chris@0: } Chris@0: else { Chris@0: $hal_settings->set('link_domain', $link_domain)->save(TRUE); Chris@0: } Chris@0: Chris@0: /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */ Chris@0: $relation_manager = \Drupal::service('hal.link_manager.relation'); Chris@0: Chris@0: $link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context); Chris@0: $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('', [], ['absolute' => TRUE])->toString(), $expected_return)); Chris@0: $this->assertEquals($context, $expected_context); Chris@0: } Chris@0: Chris@0: public function providerTestGetRelationUri() { Chris@0: $field_name = $this->randomMachineName(); Chris@0: $base_test_case = [ Chris@0: 'link_domain' => NULL, Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => $field_name, Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'site URL' => $base_test_case + [ Chris@0: 'context' => [], Chris@0: 'link_domain' => NULL, Chris@0: 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name, Chris@0: 'expected context' => [], Chris@0: ], Chris@0: // Test hook_hal_relation_uri_alter(). Chris@0: 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['hal_test' => TRUE], Chris@0: 'expected return' => 'hal_test_relation', Chris@0: 'expected context' => ['hal_test' => TRUE], Chris@0: ], Chris@0: // Test hook_rest_relation_uri_alter() — for backwards compatibility. Chris@0: 'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['rest_test' => TRUE], Chris@0: 'expected return' => 'rest_test_relation', Chris@0: 'expected context' => ['rest_test' => TRUE], Chris@0: ], Chris@0: 'configured URL' => [ Chris@0: 'link_domain' => 'http://llamas-rock.com/for-real/', Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => $field_name, Chris@0: 'context' => [], Chris@0: 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name, Chris@0: 'expected context' => [], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getRelationInternalIds Chris@0: */ Chris@0: public function testGetRelationInternalIds() { Chris@0: /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */ Chris@0: $relation_manager = \Drupal::service('hal.link_manager.relation'); Chris@0: $link = $relation_manager->getRelationUri('node', 'page', 'field_ref'); Chris@0: $internal_ids = $relation_manager->getRelationInternalIds($link); Chris@0: Chris@0: $this->assertEquals([ Chris@0: 'entity_type_id' => 'node', Chris@0: 'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'), Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => 'field_ref' Chris@0: ], $internal_ids); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::setLinkDomain Chris@0: */ Chris@0: public function testHalLinkManagersSetLinkDomain() { Chris@0: /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */ Chris@0: $link_manager = \Drupal::service('hal.link_manager'); Chris@0: $link_manager->setLinkDomain('http://example.com/'); Chris@0: $link = $link_manager->getTypeUri('node', 'page'); Chris@0: $this->assertEqual($link, 'http://example.com/rest/type/node/page'); Chris@0: $link = $link_manager->getRelationUri('node', 'page', 'field_ref'); Chris@0: $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref'); Chris@0: } Chris@0: Chris@0: }