droptica / drupal-codeception
Drupal测试的Codeception工具集。
Requires
Requires (Dev)
- composer/installers: ^2
- drupal/core: ^10
This package is not auto-updated.
Last update: 2024-09-16 13:22:50 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
提供用于通过webdriver与Drupal交互的辅助方法。
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 Fields Utility
提供用于获取特定于Drupal的表单字段xpath的xpath构建器对象。
包含:- FormField:可以设置为无限基数的目标字段 - MTOFormField:单值字段。- ParagraphFormField:段落表单字段。
用法
使用机器名称field_page_elements创建段落字段。
$page_elements = ParagraphFormField::field_page_elements();
获取下一个段落。
$page_elements->next();
从页面元素字段填充标题字段值。
$i->fillField(FormField::title($page_elements)->value);
添加一个新的提升元素类型的段落。
$i->click($page_elements->addMore('liftup_element'));
$i->waitForElementVisible($page_elements->getCurrent('Subform'));