【问题标题】:How do you setup custom fields in a unit test on Drupal 8如何在 Drupal 8 的单元测试中设置自定义字段
【发布时间】:2020-12-31 16:44:34
【问题描述】:

我正在尝试测试与用户实体上的自定义字段相关的方法的功能。 当我尝试在单元测试的 setup 方法中设置这些字段的值时,我收到错误:字段 * 未知。

我的初始测试如下所示:

namespace Drupal\my_module\Entity;
use Drupal\Tests\BrowserTestBase;
class UserTest extends BrowserTestBase
{

  /** @var \Drupal\my_module\Entity\User|false  */
  protected $user;

  protected static $modules = ['field', 'user', 'commerce_payment', 'my_module'];

  protected $strictConfigSchema = FALSE;

  public function setUp() {
    parent::setUp();
    $this->user = $this->drupalCreateUser();
    $this->user->addRole('member');
    $this->user->set('field_user_abc', [['value' => '123']]);
  }

  public function testInstance1() {
    $this->assertNotEmpty($this->user->get('field_user_abc')->get(0)->getValue());
  }

}

在 setup 方法中的 ->set() 调用失败。

如果 Drupal 使用我的项目数据库进行引导,我真的不明白为什么我需要重新创建该字段。但是,我在一些相关的帖子中读到可能是这种情况。按照/core/modules/user/tests/src/Functional/UserCreateTest.php中的例子,我尝试了以下,但结果是一样的......

namespace Drupal\my_module\Entity;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;

class UserTest extends BrowserTestBase
{

  protected $user;

  protected static $modules = ['field', 'user', 'commerce_payment', 'my_module'];

  protected $strictConfigSchema = FALSE;

  public function setUp() {
    parent::setUp();
    $this->user = $this->drupalCreateUser();
    $this->user->addRole('member');
    $this->drupalLogin($this->user);

    // Create a field.
    $field_name = 'field_user_abc';
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'user',
      'module' => 'core',
      'type' => 'string',
      'cardinality' => 1,
      'locked' => FALSE,
      'indexes' => [],
      'settings' => [
    'max_length' => 14,
    'is_ascii' => false,
    'case_sensitive' => false,
      ],
    ])->save();

    FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'user',
      'label' => 'ABC',
      'bundle' => 'user',
      'description' => '',
      'required' => FALSE,
      'settings' => [],
    ])->save();

    $this->user->set($field_name, [['value' => '123']]);

  }

  public function testInstance1() {
    $this->assertNotEmpty($this->user->get('field_user_abc')->get(0)->getValue());
  }

}

【问题讨论】:

    标签: unit-testing drupal-8


    【解决方案1】:

    这是因为您的存储类型设置为string,而不是text

    您还应该在模块依赖项中删除'module' => 'core' 并添加text

    protected static $modules = ['field', 'text', 'user', 'commerce_payment', 'my_module'];
    
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'user',
      'type' => 'text',
      'cardinality' => 1,
      'locked' => FALSE,
      'indexes' => [],
      'settings' => [
        'max_length' => 14,
        'is_ascii' => false,
        'case_sensitive' => false,
      ],
    ])->save();
    

    这将在数据库中产生一个 varchar(14)。

    导出配置后,field.storage.user.field_user_abc.yml 将如下所示:

    uuid: 2656c022-1ff2-4868-b07d-c26ff3531aac
    langcode: fr
    status: true
    dependencies:
      module:
        - text
        - user
    id: user.field_user_abc
    field_name: field_user_abc
    entity_type: user
    type: text
    settings:
      max_length: 14
      is_ascii: false
      case_sensitive: false
    module: text
    locked: false
    cardinality: 1
    translatable: true
    indexes: {  }
    persist_with_no_fields: false
    custom_storage: false
    

    【讨论】:

    • 我尝试将其更改为文本,但没有任何效果。该指令指的是值应如何存储在数据库中,因此“字符串”类型是有意义的,而“文本”类型通常用于可渲染定义中,指的是表单字段类型。谢谢。
    • 我尝试删除模块并更改模块以匹配我的自定义模块。两者都没有效果。
    • 我编辑了我的答案,看看它。您需要在模块列表中添加一个依赖项;)
    • 感谢您的时间@MacSim。我仍然看到同样的错误。我需要 FieldConfig::create() 吗?也许还有其他事情发生。 ` 受保护的静态 $modules = ['field', 'user', 'text', 'commerce_payment', 'my_module']; // ... FieldStorageConfig::create([ 'field_name' => 'field_common_abc', 'entity_type' => 'user', 'module' => 'text', 'type' => 'text', 'cardinality' => 1, 'locked' => FALSE, 'indexes' => [], 'translatable' => FALSE, 'settings' => [ 'max_length' => 14, 'is_ascii' => FALSE, 'case_sensitive' = > 错误, ], ])->保存(); `
    • 是的,你需要FieldStorageConfigFieldConfig。我从 module.install 以编程方式创建了该字段,从后台和外壳安装模块没有任何问题。测试应该是一样的……很奇怪。也许您应该尝试在$this->user = $this->drupalCreateUser(); 之前创建该字段
    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2020-05-06
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多