logo

额外区块类型 (EBT) - 全新的布局构建器体验❗

额外区块类型 (EBT) - 样式化、可定制的区块类型:幻灯片、标签页、卡片、手风琴等更多类型。内置背景、DOM Box、JavaScript 插件的设置。立即体验布局构建的未来。

演示 EBT 模块 下载 EBT 模块

❗额外段落类型 (EPT) - 全新的 Paragraphs 体验

额外段落类型 (EPT) - 类似的基于 Paragraph 的模块集合。

演示 EPT 模块 滚动

滚动
03/10/2025, by Ivan

第六部分 Drupal 8 基础模块构建实用指南
从 .info 到测试,只讲基础

如果你从一开始就跟随这个 Drupal 8 基础模块构建实用指南,那么现在我们准备进行一些质量控制。如果你只是想跳过并直接上手,你可以从 Drupal 下载 Lorem ipsum 模块

让我们通过编写几个可以直接在 Drupal 中运行的自定义测试,来检查模块是否按预期工作。

/tests/src/Functional/LoremIpsumTest.php

<?php

namespace Drupal\Tests\loremipsum\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests for the Lorem Ipsum module.
 *
 * @group loremipsum
 */
class LoremIpsumTests extends BrowserTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = array('loremipsum');

  /**
   * A simple user.
   *
   * @var \Drupal\user\Entity\User
   */
  private $user;

  /**
   * Perform initial setup tasks that run before every test method.
   */
  public function setUp() {
    parent::setUp();
    $this->user = $this->drupalCreateUser(array(
      'administer site configuration',
      'generate lorem ipsum',
    ));
  }
}

测试从继承 BrowserTestBase 类开始。需要注意的是,Drupal 会在测试数据库中运行测试,这个数据库会被创建、按需填充,然后销毁。这意味着你可以在开发、预发布,甚至——如果你敢的话!——生产环境中进行测试。

正如你在上面看到的,测试的第一步是进行一些初始设置:定义要测试的模块(这里只测试我们自己的模块)、创建测试用户并赋予必要的权限。

然后我们测试对文本生成页面的访问:

/**
   * Tests that the Lorem ipsum page can be reached.
   */
  public function testLoremIpsumPageExists() {
    // 登录。
    $this->drupalLogin($this->user);

    // 生成器测试:
    $this->drupalGet('loremipsum/generate/4/20');
     $this->assertSession()->statusCodeEquals(200);
  }

以及配置表单:

  /**
   * Tests the config form.
   */
  public function testConfigForm() {
    // 登录。
    $this->drupalLogin($this->user);

    // 访问配置页面。
    $this->drupalGet('admin/config/development/loremipsum');
     $this->assertSession()->statusCodeEquals(200);
    // 测试表单元素是否存在并具有默认值。
    $config = $this->config('loremipsum.settings');
    $this->assertSession()->fieldValueEquals(
      'page_title',
      $config->get('loremipsum.page_title'),
    );
    $this->assertSession()->fieldValueEquals(
      'source_text',
      $config->get('loremipsum.source_text'),
    );

接着我们测试配置表单是否可以提交:

   // 测试表单提交。
    $this->drupalPostForm(NULL, array(
      'page_title' => 'Test lorem ipsum',
      'source_text' => 'Test phrase 1 \nTest phrase 2 \nTest phrase 3 \n',
    ), t('Save configuration'));
    $this->assertSession()->pageTextContains('The configuration options have been saved.');

并断言新值是否生效:

    // 测试新值是否存在。
    $this->drupalGet('admin/config/development/loremipsum');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->fieldValueEquals(
      'page_title',
      'Test lorem ipsum',
    );
    $this->assertSession()->fieldValueEquals(
      'source_text',
      'Test phrase 1 \nTest phrase 2 \nTest phrase 3 \n',
    );
  }

}

运行测试的步骤:
1. 启用核心模块:Testing 和 Lorem ipsum
2. 进入 admin/config/development/testing
3. 选择 “Loremipsum” 测试
4. 点击 “运行测试”

本课就到这里!如果你愿意,可以在 Lorem ipsum 项目页面获取这段代码的副本——请注意,当前开发版本包含了内置的主题支持。此外,如果你有任何问题,欢迎随时联系我。祝编码愉快!