wunderio/drupal-codeception

Drupal 测试的 Codeception 工具集。

安装量: 7,858

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 20

类型:软件包

9.0.2 2020-11-09 17:03 UTC

README

概述

包含一系列用于测试 Drupal CMS 的 Codeception 模块和实用工具。包括

  • Drupal Bootstrap
  • Drupal Entity
  • Drupal User
  • Drupal Watchdog
  • Drupal Drush
  • Drupal Acceptance

安装

需要包

composer require guncha25/drupal-codeception --dev

如果之前没有设置 codeception

./vendor/bin/codecept bootstrap

如果您现在只需要一个测试套件(例如接受测试)

./vendor/bin/codecept init acceptance

Drupal Bootstrap

在测试前完全启动到 Drupal。允许在测试用例中使用 Drupal API。

配置

  • server: 服务器和执行环境信息。
  • root: Drupal 根目录。使用 DrupalFinder 检测 Drupal 根目录。如果找不到,则回退到 codeception 根目录 + /web。(可选)
modules:
    - DrupalBootstrap:
        server:
            SERVER_PORT: null
            REQUEST_URI: '/'
            REMOTE_ADDR: '127.0.0.1'
            HTTP_HOST: 'site.multi'

Drupal Drush

提供 drush(《runDrush》)命令。

配置

  • working_directory: drush 应该执行的当前工作目录。默认为 codeception 根目录。
  • drush: Drush 可执行文件。默认为 drush
modules:
    - DrupalDrush:
        working_directory: './web'
        drush: './vendor/bin/drush'
          options:
            uri: http://mydomain.com
            root: /app/web

用法

运行 drush config import 并存储输出。

$output = $i->runDrush('cim -y');

获取一次性登录 URL。

$uri = $i->getLoginUri('userName');

Drupal Entity

提供与 Drupal 实体和测试实体清理服务的更好交互。

配置

  • cleanup_test: 指示测试实体是否应该在每次测试后删除。
  • cleanup_failed: 指示测试实体是否应该在测试失败后删除。
  • cleanup_suite: 指示测试实体是否应该在套件后删除。
  • route_entities: 可以通过 URL 获取的实体列表。
modules:
    - DrupalEntity:
        cleanup_test: true
        cleanup_failed: false
        cleanup_suite: true
        route_entities:
            - node
            - taxonomy_term

用法

创建实体。

$node = $i->createEntity(['title => 'My node', 'type' => 'page']);

$term = $i->createEntity(['name => 'My term', 'vid' => 'tag'], 'taxonomy_term');

删除所有存储的测试实体。

$i->doEntityCleanup();

注册测试实体。

$i->registerTestEntity('node', '99');

通过 URL 注册测试实体。

$i->registerTestEntityByUrl($i->grabFromCurrentUrl());

通过 URL 获取实体。

$entity = $i->getEntityFromUrl($i->grabFromCurrentUrl());

Drupal User

提供与 Drupal 用户和测试用户设置和清理服务的更好交互。

配置

  • default_role: 未指定时默认用户角色。默认为 'authenticated'
  • driver: 用于与站点交互的驱动程序。默认为 WebDriver。
  • drush: Drush 可执行文件。默认为 drush
  • cleanup_entities: 当测试用户被删除时删除的实体。
  • cleanup_test: 指示测试实体是否应该在每次测试后删除。
  • cleanup_failed: 指示测试实体是否应该在测试失败后删除。
  • cleanup_suite: 指示测试实体是否应该在套件后删除。
modules:
    - DrupalUser:
        default_role: 'authenticated'
        driver: 'PhpBrowser'
        drush: './vendor/bin/drush'
        cleanup_entities:
            - media
            - file
        cleanup_test: true
        cleanup_failed: false
        cleanup_suite: true

用法

创建具有指定角色的测试用户。

$user = $i->createUserWithRoles(['editor', 'authenticated'], 'password');

通过用户名登录用户。

$i->loginAs('userName');

创建具有特定角色的新用户并登录。

$i->logInWithRole('administrator');

Drupal Watchdog

在测试时提供日志检查。

配置

  • enabled: 套件后是否启用自动检查。默认为 TRUE
  • level: 产生失败的全球日志级别。默认为 'ERROR'。
  • channels: 个别日志通道设置。
modules:
    - DrupalWatchdog:
        enabled: true
        level: 'ERROR'
        channels:
            my_module: 'NOTICE'
            php: 'WARNING'

用法

清理日志。

$i->prepareLogWatch();

检查日志。

$i->checkLogs();

Drupal Acceptance

提供与 Drupal 通过 WebDriver 交互的辅助方法。

modules:
    - DrupalAcceptance:

用法

// Fill title.
$i->fillTextField(FormField::title(), 'Mans nosukums');

// Select english language for content.
$i->selectOptionFromList(FormField::langcode(), 'en');

// Fill first paragraph of type text.
$page_elements = ParagraphFormField::field_page_elements();
$i->fillWysiwygEditor(FormField::field_text_formatted($page_elements), 'Lorem');

// Remove text paragraph.
$i->removeParagraph($page_elements);

// Add second paragraph of type graybox.
$i->addParagraph('graybox', $page_elements->next());

// Fill title of graybox.
$i->fillTextField(FormField::field_title($page_elements), 'Mans nosukums');

// Fill link field.
$i->fillLinkField(FormField::field_link($page_elements), 'http://example.com', 'Example');
$i->fillLinkField(FormField::field_link($page_elements), 'http://example.com');

// Add taxonomy term reference for tags field.
$field_tags = FormField::field_tags();
$i->fillReferenceField(FormField::field_tags(), Fixtures::get('term1'));
$i->addReferenceFieldItem($field_tags->next());
$i->fillReferenceField($field_tags, Fixtures::get('term2'));

$i->clickOn(FormField::submit());

Drupal 字段工具

提供用于检索特定于 Drupal 的表单字段 XPath 的 XPath 构建器对象。

包括:- FormField: 可以设置为无限基数性的字段 - MTOFormField: 单值字段。 - ParagraphFormField: 段落表单字段。

用法

创建具有机器名称 field_page_elements 的段落字段。

$page_elements = ParagraphFormField::field_page_elements();

获取下一个段落。

$page_elements->next();

从 field_page_elements 中填充标题字段值。

$i->fillField(FormField::title($page_elements)->value);

添加一个新的提升元素类型的段落。

$i->click($page_elements->addMore('liftup_element'));
$i->waitForElementVisible($page_elements->getCurrent('Subform'));