上一页下一页/drupal-test-utils

Drupal 测试工具特性。

0.0.4 2024-05-21 00:22 UTC

This package is auto-updated.

Last update: 2024-09-23 06:31:59 UTC


README

Drupal 测试工具!

ConfigTrait

一个特性,用于在测试期间覆盖配置。

使用方法

将特性添加到基类并覆盖 tearDown 方法。

use PNX\DrupalTestUtils\Traits\ConfigTrait;
use weitzman\DrupalTestTraits\ExistingSiteBase;

abstract class MyBaseClass extends ExistingSiteBase {

  /**
   * {@inheritdoc}
   */
  protected function tearDown(): void {
    $this->setConfigValues($this->originalConfiguration, FALSE);
    parent::tearDown();
  }

}

在测试用例中,调用 $this->setConfigValues

$this->setConfigValues([
  'system.logging' => [
    'error_level' => \ERROR_REPORTING_DISPLAY_VERBOSE,
  ],
]);

EntityLoadTrait

一个特性,用于在测试中辅助加载实体。

使用方法

加载标题为 "Hello, World" 的一个节点

$node = $this->loadEntityByProperty('node', ['title' => 'Hello, World']);

加载所有文章节点

$nodes = $this->loadEntityByProperty('node', ['type' => 'article'], FALSE);

获取最后创建的节点。对于通过 UI 创建的实体断言非常有用

$node = $this->getLastCreatedEntity('node');

ExpectsCacheableResponseTrait

一个特性,用于在功能测试中的每个请求添加动态页面缓存缓存性测试。

使用方法

一旦特性添加到测试基类,您可以通过覆盖 drupalGet 函数来检查缓存性。

/**
 * {@inheritdoc}
 */
protected function drupalGet($path, array $options = [], array $headers = []): string {
  $response = parent::drupalGet($path, $options, $headers);
  $this->detectUncacheableResponse($path, $options);
  return $response;
}

标记路径为不可缓存

有一些路径始终是不可缓存的(例如,带有表单的页面,如 node/add)。可以通过将 $uncacheableDynamicPagePatterns 属性添加到测试中来标记这些路径为不可缓存。您可以将这些常用路径添加到测试基类,并在单个测试中添加更多特定路径,因为这些模式将在运行时从类层次结构中收集。

模式与 preg_match 匹配的正则表达式

例如

 protected static array $uncacheableDynamicPagePatterns = [
   '/user/login',
   '/big_pipe/no-js',
   '/batch',
   '/node/add/.*',
 ];