UriItemTest.php

Same filename in other branches
  1. 9 core/modules/field/tests/src/Kernel/Uri/UriItemTest.php
  2. 10 core/modules/field/tests/src/Kernel/Uri/UriItemTest.php
  3. 10 core/tests/Drupal/Tests/Core/Field/UriItemTest.php
  4. 11.x core/modules/field/tests/src/Kernel/Uri/UriItemTest.php
  5. 11.x core/tests/Drupal/Tests/Core/Field/UriItemTest.php

Namespace

Drupal\Tests\field\Kernel\Uri

File

core/modules/field/tests/src/Kernel/Uri/UriItemTest.php

View source
<?php

namespace Drupal\Tests\field\Kernel\Uri;

use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;

/**
 * Tests URI field functionality.
 *
 * @see \Drupal\Core\Field\Plugin\Field\FieldType\UriItem
 *
 * @group field
 */
class UriItemTest extends FieldKernelTestBase {
    
    /**
     * A field to use in this test class.
     *
     * @var \Drupal\field\Entity\FieldStorageConfig
     */
    protected $fieldStorage;
    
    /**
     * The field used in this test class.
     *
     * @var \Drupal\field\Entity\FieldConfig
     */
    protected $field;
    
    /**
     * Tests URI field.
     */
    public function testUriField() {
        $label = $this->randomMachineName();
        // Create a field with settings to validate.
        $field_name = mb_strtolower($this->randomMachineName());
        $this->fieldStorage = FieldStorageConfig::create([
            'field_name' => $field_name,
            'entity_type' => 'entity_test',
            'type' => 'uri',
        ]);
        $this->fieldStorage
            ->save();
        $this->field = FieldConfig::create([
            'field_name' => $field_name,
            'entity_type' => 'entity_test',
            'bundle' => 'entity_test',
            'label' => $label,
            'required' => TRUE,
            'settings' => [
                'size' => 123,
                'placeholder' => '',
            ],
        ]);
        $this->field
            ->save();
        // Create a form display for the default form mode.
        \Drupal::service('entity_display.repository')->getFormDisplay('entity_test', 'entity_test')
            ->setComponent($field_name, [
            'type' => 'uri',
        ])
            ->save();
        // Test the generateSampleValue() method.
        $entity = EntityTest::create();
        $entity->{$field_name}
            ->generateSampleItems();
        $this->entityValidateAndSave($entity);
    }

}

Classes

Title Deprecated Summary
UriItemTest Tests URI field functionality.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.