guncha25 / drupal-codeception
Drupal 测试的 Codeception 工具集。
Requires
- codeception/codeception: ^5
- codeception/module-asserts: ^3
- codeception/module-phpbrowser: ^3
- codeception/module-webdriver: ^3 || ^4
- fakerphp/faker: ^1.23
- webflo/drupal-finder: ^1.2
Requires (Dev)
- composer/installers: ^2
- drupal/core: ^9 || ^10
README
概述
包含用于测试 Drupal CMS 的代码ception 模块和实用工具集。包括
- 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 根目录。
- timeout: drush 命令的秒数超时。设置为 0 表示无超时。默认为 60 秒。
- drush: Drush 可执行文件。默认为
drush
。
modules:
- DrupalDrush:
working_directory: './web'
timeout: 120
drush: './vendor/bin/drush'
options:
uri: http://mydomain.com
root: /app/web
使用
运行 drush 状态并存储输出。
$output = $i->runDrush('status');
运行 drush 配置导入并存储来自 STDERR 的输出。
$output = $i->runDrush('cim -y', [], TRUE)->getErrorOutput();
运行 drush 缓存重建并存储退出代码。
$exit_code = $i->runDrush('cr', [], TRUE)->getExitCode();
获取一次性登录 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' => '我的节点', 'type' => 'page']);
$term = $i->createEntity(['name' => '我的术语', '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 option from select list by key or value.
// For custom fields, target (last parameter) usually needs to be set to an
// empty string.
$i->selectOptionFromList(FormField::langcode(), 'en');
$i->selectOptionFromList(FormField::langcode(), 'English');
$i->selectOptionFromList(FormField::field_my_list(), 'Apple', '');
// Select the nth option from a select list.
$i->selectOptionFromList(FormField::langcode());
$i->selectNthOptionFromList(MTOFormField::field_my_list(), 2, '');
// 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();
从字段 page elements 中填充标题字段值。
$i->fillField(FormField::title($page_elements)->value);
添加新的类型为 liftup_element 的段落。
$i->click($page_elements->addMore('liftup_element'));
$i->waitForElementVisible($page_elements->getCurrent('Subform'));