Chris@0: 'field_test', Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'link', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'field_name' => 'field_test', Chris@0: 'bundle' => 'entity_test', Chris@0: 'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC], Chris@0: ])->save(); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => 'field_test_external', Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'link', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'field_name' => 'field_test_external', Chris@0: 'bundle' => 'entity_test', Chris@0: 'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL], Chris@0: ])->save(); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => 'field_test_internal', Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'link', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'field_name' => 'field_test_internal', Chris@0: 'bundle' => 'entity_test', Chris@0: 'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL], Chris@0: ])->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests using entity fields of the link field type. Chris@0: */ Chris@0: public function testLinkItem() { Chris@0: // Create entity. Chris@0: $entity = EntityTest::create(); Chris@0: $url = 'https://www.drupal.org?test_param=test_value'; Chris@0: $parsed_url = UrlHelper::parse($url); Chris@0: $title = $this->randomMachineName(); Chris@0: $class = $this->randomMachineName(); Chris@0: $entity->field_test->uri = $parsed_url['path']; Chris@0: $entity->field_test->title = $title; Chris@0: $entity->field_test->first()->get('options')->set('query', $parsed_url['query']); Chris@0: $entity->field_test->first()->get('options')->set('attributes', ['class' => $class]); Chris@0: $this->assertEquals([ Chris@0: 'query' => $parsed_url['query'], Chris@0: 'attributes' => [ Chris@0: 'class' => $class, Chris@0: ], Chris@0: 'external' => TRUE, Chris@0: ], $entity->field_test->first()->getUrl()->getOptions()); Chris@0: $entity->name->value = $this->randomMachineName(); Chris@0: $entity->save(); Chris@0: Chris@0: // Verify that the field value is changed. Chris@0: $id = $entity->id(); Chris@0: $entity = EntityTest::load($id); Chris@0: $this->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.'); Chris@0: $this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.'); Chris@0: $this->assertEqual($entity->field_test->uri, $parsed_url['path']); Chris@0: $this->assertEqual($entity->field_test[0]->uri, $parsed_url['path']); Chris@0: $this->assertEqual($entity->field_test->title, $title); Chris@0: $this->assertEqual($entity->field_test[0]->title, $title); Chris@0: $this->assertEqual($entity->field_test->options['attributes']['class'], $class); Chris@0: $this->assertEqual($entity->field_test->options['query'], $parsed_url['query']); Chris@0: Chris@0: // Update only the entity name property to check if the link field data will Chris@0: // remain intact. Chris@0: $entity->name->value = $this->randomMachineName(); Chris@0: $entity->save(); Chris@0: $id = $entity->id(); Chris@0: $entity = EntityTest::load($id); Chris@0: $this->assertEqual($entity->field_test->uri, $parsed_url['path']); Chris@0: $this->assertEqual($entity->field_test->options['attributes']['class'], $class); Chris@0: $this->assertEqual($entity->field_test->options['query'], $parsed_url['query']); Chris@0: Chris@0: // Verify changing the field value. Chris@0: $new_url = 'https://www.drupal.org'; Chris@0: $new_title = $this->randomMachineName(); Chris@0: $new_class = $this->randomMachineName(); Chris@0: $entity->field_test->uri = $new_url; Chris@0: $entity->field_test->title = $new_title; Chris@0: $entity->field_test->first()->get('options')->set('query', NULL); Chris@0: $entity->field_test->first()->get('options')->set('attributes', ['class' => $new_class]); Chris@0: $this->assertEqual($entity->field_test->uri, $new_url); Chris@0: $this->assertEqual($entity->field_test->title, $new_title); Chris@0: $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); Chris@0: $this->assertNull($entity->field_test->options['query']); Chris@0: Chris@0: // Read changed entity and assert changed values. Chris@0: $entity->save(); Chris@0: $entity = EntityTest::load($id); Chris@0: $this->assertEqual($entity->field_test->uri, $new_url); Chris@0: $this->assertEqual($entity->field_test->title, $new_title); Chris@0: $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); Chris@0: Chris@0: // Check that if we only set uri the default values for title and options Chris@0: // are also initialized. Chris@0: $entity->field_test = ['uri' => 'internal:/node/add']; Chris@0: $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); Chris@0: $this->assertNull($entity->field_test->title); Chris@0: $this->assertIdentical($entity->field_test->options, []); Chris@0: Chris@18: // Check that if we set uri and options then the default values are properly Chris@18: // initialized. Chris@0: $entity->field_test = [ Chris@0: 'uri' => 'internal:/node/add', Chris@18: 'options' => ['query' => NULL], Chris@0: ]; Chris@0: $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); Chris@0: $this->assertNull($entity->field_test->title); Chris@0: $this->assertNull($entity->field_test->options['query']); Chris@0: Chris@0: // Check that if we set the direct value of link field it correctly set the Chris@0: // uri and the default values of the field. Chris@0: $entity->field_test = 'internal:/node/add'; Chris@0: $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); Chris@0: $this->assertNull($entity->field_test->title); Chris@0: $this->assertIdentical($entity->field_test->options, []); Chris@0: Chris@0: // Check that setting options to NULL does not trigger an error when Chris@0: // calling getUrl(); Chris@0: $entity->field_test->options = NULL; Chris@0: $this->assertInstanceOf(Url::class, $entity->field_test[0]->getUrl()); Chris@0: Chris@0: // Check that setting LinkItem value NULL doesn't generate any error or Chris@0: // warning. Chris@0: $entity->field_test[0] = NULL; Chris@0: $this->assertNull($entity->field_test[0]->getValue()); Chris@0: Chris@0: // Test the generateSampleValue() method for generic, external, and internal Chris@0: // link types. Chris@0: $entity = EntityTest::create(); Chris@0: $entity->field_test->generateSampleItems(); Chris@0: $entity->field_test_external->generateSampleItems(); Chris@0: $entity->field_test_internal->generateSampleItems(); Chris@0: $this->entityValidateAndSave($entity); Chris@0: } Chris@0: Chris@18: /** Chris@18: * Tests the deprecated behavior of LinkItem::setValue(). Chris@18: * Chris@18: * @group legacy Chris@18: * @expectedDeprecation Support for passing options as a serialized string is deprecated in 8.7.0 and will be removed before Drupal 9.0.0. Pass them as an array instead. See https://www.drupal.org/node/2961643. Chris@18: */ Chris@18: public function testSerializedOptions() { Chris@18: // Check that if we set uri and options then the default values are Chris@18: // properly initialized. Chris@18: $entity = EntityTest::create(); Chris@18: $entity->set('field_test', [ Chris@18: 'uri' => 'internal:/node/add', Chris@18: 'options' => serialize(['query' => NULL]), Chris@18: ]); Chris@18: $this->assertEquals('internal:/node/add', $entity->get('field_test')->uri); Chris@18: $this->assertNull($entity->get('field_test')->title); Chris@18: $this->assertNull($entity->get('field_test')->options['query']); Chris@18: } Chris@18: Chris@0: }