coldfrontlabs / drupal-codeception
用于 Drupal 测试的 Codeception 工具集。
Requires
- codeception/codeception: ^4.0 || ^5.0
- codeception/module-webdriver: *
- drupal/webprofiler: ^9 || ^10
- webflo/drupal-finder: *
Requires (Dev)
- composer/installers: *
- drupal/core: ^9 || ^10
This package is auto-updated.
Last update: 2024-09-19 20:12:06 UTC
README
概览
包含一系列 Codeception 模块和工具,用于测试 Drupal CMS。包括
- Drupal Bootstrap
- Drupal Entity
- Drupal User
- Drupal Watchdog
- Drupal Drush
- Drupal Acceptance
与 guncha25/drupal-codeception 的不同之处
- PHP 8 支持
- 不依赖 Faker
- 各种修复
安装
需要包
composer require coldfrontlabs/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' => '我的节点', '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 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();
从字段 page_elements 中填充标题字段值。
$i->fillField(FormField::title($page_elements)->value);
添加新的类型为 liftup_element 的段落。
$i->click($page_elements->addMore('liftup_element'));
$i->waitForElementVisible($page_elements->getCurrent('Subform'));