nucivic/dkanextension

为 Drupal 扩展提供基础 DKAN 类


README

已弃用: 此模块已移至 DKAN 核心库,版本 7.x-1.13。为了保持向后兼容性,此项目将继续保留在 Github 上,但新功能将直接应用于 DKAN 核心文件夹 test/dkanextension

Behat DKAN 上下文

这为 DKAN 和 NuCivic 特定步骤创建了一个功能上下文。

安装

  1. 创建以下内容的 composer.json 文件
{
  "require": {
    "nucivic/dkanextension": "dev-master"
  },
  "config": {
    "bin-dir": "bin/"
  },
}
  1. 安装依赖项: composer install

  2. 初始化: behat --init

上下文

DKANExtension 随带许多“上下文”或类,Behat 使用这些上下文添加步骤函数或其他功能。默认情况下,不会加载这些上下文中的任何一个。您需要将您想要使用的每个上下文添加到您的 behat.yml 文件中。以下是一个添加所有上下文的示例,但您也可以选择只加载您想要的上下文。如果您想要覆盖这些上下文并使用自定义版本,这非常有用。如果您已经使用了 DKANExtension 所依赖的 DrupalExtension,它的工作方式相同。

设置您的 behat.yml

default:
  suites:
     default:
        contexts:
          - FeatureContext
          # Load the Drupal Context from DrupalExtension
          - Drupal\DrupalExtension\Context\DrupalContext
          # Load the generic DKAN context
          - Drupal\DKANExtension\Context\DKANContext
          # Load DKAN Groups functionality
          - Drupal\DKANExtension\Context\GroupContext
          # Load DevinciExtension debug functionality
          - Devinci\DevinciExtension\Context\DebugContext:
              asset_dump_path: %paths.base%/../assets/
          # Set the default max wait time when testing JS "wait for.." tests.
          - Devinci\DevinciExtension\Context\JavascriptContext:
              maximum_wait: 30
...

可用的上下文

所有上下文都在 src/Drupal/DKANExtension/Context 文件夹中。我们只显示您在 behat.yml 中考虑加载的上下文以及它们提供的自定义步骤。

DKANContext.php

包含一些辅助步骤的通用上下文

  • 将默认 php 时区设置为 date_default_timezone_set('America/New_York');
  • @When 我搜索 :term
  • @Then /^我应该看到管理菜单$/
  • @Then /^我应该有一个 "([^"]*)" 文本格式选项$/

DKANDataStoryContext.php

扩展 RawDKANEntityContext

  • @Given 数据故事

    Data Story Field Mappings:
    'title' => 'title' - Title of the data story (string)
    'author' => 'author' - Author of the data story (username)
    'status' => 'status', - Is this published or not ('Yes', 'No')
    'description' => 'body' - Description of the data story (string)
    'tags' => 'field_tags' - Tags (comma separated strings)
    

    示例

    Given data stories:
      | author    | title     | description       | status | tags     |
      | TestUser  | Story 01  | Some Description  | Yes    | some tag |
    And I am on the "Story 01" page

DataDashboardContext.php

扩展 RawDKANEntityContext

  • @Given 数据仪表板

    Dashboard Field Mappings:
    'title' => 'title' - Title of the Data Dashboard (string)
    

    示例

    Given data dashboards:
      | title         |
      | Dashboard 01  |

DatasetContext.php

扩展 RawDKANEntityContext

  • @Given 数据集

    Dataset Field Mappings:
    'author' => 'author' - author of the Dataset (username)
    'title' => 'title' - Dataset title (string)
    'description' => 'body' - Dataset description (string)
    'publisher' => 'og_group_ref' - Group name (string)
    'published' => 'status' - Published? ('Yes', 'No')
    'tags' => 'field_tags' - Tags to add (comma separated strings)
    

    示例

    Given groups:
      | author    | title     | published     |
      | TestUser  | Group 01  | Yes           |
    Given datasets:
      | author    | title       | description       | publisher  | published | tags     |
      | TestUser  | Dataset 01  | Some Description  | Group 01   | Yes       | some tag |
    And I am on the "Dataset 01" page
  • @Then 我应该看到一个名为 :text 的数据集

    • :text - 数据集的标题(字符串)

    示例

    Given groups:
      | author    | title     | published     |
      | TestUser  | Group 01  | Yes           |
    Given datasets:
      | author    | title       | description       | publisher  | published | tags     |
      | TestUser  | Dataset 01  | Some Description  | Group 01   | Yes       | some tag |
    And I am on the "Group 01" page
    Then I should see a dataset called "Dataset 01"
  • @Then 数据集搜索在幕后更新

    (已弃用)

GroupContext.php

扩展 RawDKANEntityContext

  • @Given 群组

    Group Field Mappings:
    'author' => 'author' -  Author of the Group (username)
    'title' => 'title' - Group Name (string)
    'published' => 'status' - Published? ('Yes', 'No')
    

    示例

    Given groups:
      | author    | title     | published     |
      | TestUser  | Group 01  | Yes           |
    And I am on the "Group 01" page
  • @Given 群组成员资格

    将用户分配为具有特定角色的群组成员。

    Table fields:
    'user' - (username)
    'group' - Group Name (string)
    'role on group' - ('administrator member', 'member')
    'membership status' - What is the user's group status? ('Active')
    

    示例

    Given groups:
      | author    | title     | published     |
      | TestUser  | Group 01  | Yes           |
    Given group memberships
      | user       | group     | role on group        | membership status |
      | TestUser   | Group 01  | administrator member | Active            |
  • @Given /^我是群组 "([^"]*)" 中的 "([^"]*)"$/

    将给定的角色授予当前用户,用于给定的群组。

    • :role - 群组中的角色。请参阅 @Given 群组成员资格:
    • :group - 群组名称(字符串)

    示例

    Given groups:
      | author    | title     | published     |
      | TestUser  | Group 01  | Yes           |
    And I am a "administrator member" of the group "Group 01"

MailContext.php

  • 创建 @mail 标签以用于测试场景中的电子邮件通知

(目前没有其他步骤)

PageContext.php

  • @Given 页面

    Table Fields:
    title - Page Name (string) -- Must be unique per scenario.
    url - Relative Path ('/my-page') or full url ('https://facebook.com')
    

    示例

    @Given pages:
      | title        | url    |
      | Content List | /node  |
  • @Given 我在(的):page 页面上

    更改当前页面。了解由其他上下文(如 GroupContext)创建的页面。

    • :page - 由 @Given 页面 或其他与 PageContext 集成的上下文创建的页面的标题

    示例

    手动创建的页面

    Given pages:
      | title        | url    |
      | Content List | /node  |
    And I am on the "Content List" page
    Then I should see "Content"

    使用由其他上下文创建的页面。

    Given groups:
      | author    | title     | published     |
      | TestUser  | Group 01  | Yes           |
    And I am on the "Group 01" page

ResourceContext.php

扩展 RawDKANEntityContext

  • @Given 资源

    Resource Field Mappings:
    'author' => 'author' - Username of user (string)
    'title' => 'title' - Resource title (string)
    'description' => 'body' - Resource Description (string)
    'publisher' => 'og_group_ref' - Group name (string)
    'published' => 'status' - ('Yes', 'No')
    'resource format' => 'field_format' - ('CSV', 'XML')
    'dataset' => 'field_dataset_ref' - Dataset name (string)
    

关于 RawDKANEntityContext

RawDKANEntityContext 是一个基类,其他内容/实体上下文都从此类扩展。它可以用于轻松创建具有以下功能的新上下文

  • 在拆除时自动删除内容。
  • 处理任何实体(节点+)
  • 在PageContext中创建“页面”,以便在创建后可以导航到某个项。
  • 处理“人类”字段标签到drupal字段名的映射,并处理使用表格添加实体。
  • 处理用户名到uid的作者映射。
  • 在整体中使用实体API元数据包装器,使得设置字段以一致的方式变得更容易。
  • 在$entities[$id]中保持创建的实体列表,用于拆卸和其他用途,如按名称查找实体。

您应该使用现有的“实体上下文”,或者为您的项目创建一个新的上下文,而不是使用缺少大多数这些功能的DrupalExtension的@Given content:。因为实体API需要启动,所以在使用相关步骤时,您只能使用'drupal driver',而不能使用'drush'或'blackbox'。

创建新的实体上下文

要为任何实体(和节点)创建新的上下文,在您的/bootstrap文件夹中创建一个新的上下文。在这个例子中,我们假设实体是一个名为“myentity”的节点类型。将文件命名为MyEntityContext.php(其中MyEntity是您的实体名称。)您可以使用这个模板开始。

<?php
namespace Drupal\DKANExtension\Context;
use Behat\Gherkin\Node\TableNode;
/**
 * Defines application features from the specific context.
 */
class MyEntityContext extends RawDKANEntityContext{

  public function __construct(){
    parent::__construct(array(
      // These are the mappings of human readable name => drupal field machine name or entity property name.
      'title' => 'title',
      'author' => 'author',
      'myfield' => 'field_my_field'
    ),
      // This is the bundle name if one exists, or null if one doesn't.
      // for nodes bundle == node_type name
      'myentity',
      // This is the entity type.
      'node'
    );
  }

  /**
   * @Given myentities:
   */
  public function addDataDashboard(TableNode $dashboardtable){
    //This is an example of calling parent helper functions.
    parent::addMultipleFromTable($dashboardtable);
  }

  /**
   * A contrived exmple of hooking into the Entity helpers by overriding them.
   *
   * Set all of the entities to be unpublished before they're created, no matter what.
   */
  public function wrap($entity){
    $wrapper = parent:wrap($wrap);
    $wrapper->status = 0;
    return $wrapper;
  }


  /**
   * @Given myentities:
   *
   * All custom entities will almost all want to add this type of function.
   */
  public function addDataDashboard(TableNode $dashboardtable){
    //This is an example of calling parent helper functions.
    parent::addMultipleFromTable($dashboardtable);
  }

  /**
   * @Given there should be ":number" MyEntities.
   *
   * An example showing how you can creat new step functions and get the entities array from the parent class.
   */
  public function assertNumberEntities($number){
    $actual = count($this->entities);
    if ($number !== count($this->entities) {
      throw new Exception("Asserted that there are $number myEntites, but there are actually $actual.");
    }
  }
}

现在像添加任何新自定义上下文一样将新的实体上下文添加到您的behat.yml中

default:
  suites:
     default:
        contexts:
          - FeatureContext
          # Add the new entity context
          - MyEntityContext
...

待办事项

  • 确保脚本在安装时工作
  • 添加测试
  • 在DKAN和相关模块上部署