Skip to content

Instantly share code, notes, and snippets.

@pfaocle
Created March 11, 2016 09:50
Show Gist options
  • Save pfaocle/cd5846d1db8b8421bdf1 to your computer and use it in GitHub Desktop.
Save pfaocle/cd5846d1db8b8421bdf1 to your computer and use it in GitHub Desktop.
Generic Codeception Cest for testing content types and fields with Drupal Content Type Registry
<?php
use \AcceptanceTester\AuthenticatedSteps;
use \Codeception\Module\Drupal\ContentTypeRegistry\ContentType;
use \Codeception\Module\Drupal\Pages\AdminContentTypesPage;
use \Codeception\Module\Drupal\Pages\AdminManageFieldsPage;
/**
* Test defined content types and fields.
*
* @guy AcceptanceTester\AuthenticatedSteps
*/
class ContentTypesCest
{
/**
* Test the page at admin/structure/types.
*
* @param AuthenticatedSteps $I
*/
public function testContentTypeAdminPage(AuthenticatedSteps $I)
{
$I->wantTo('ensure that all content types are present on the content types page');
$I->amGoingTo('log in and head to the content types admin page');
$I->login($I->getUserByRole('administrator'));
$I->amOnPage(AdminContentTypesPage::route());
foreach ($I->getContentTypes() as $contentType) {
$I->expectTo('see that the ' . $contentType->getMachineName() . ' type is on the content types page');
$I->see(sprintf('Machine name: %s', $contentType->getMachineName()), '//table//td');
$I->see($contentType->getHumanName(), '//table//td');
}
$I->logout();
}
/**
* Test for all content type fields on the administration pages.
*
* @param AuthenticatedSteps $I
*/
public function testFields(AuthenticatedSteps $I)
{
$I->wantTo('ensure that all fields are present on all content types on the admin side');
$I->amGoingTo('log in and head to the content types admin page');
$I->login($I->getUserByRole('administrator'));
$I->amOnPage(AdminContentTypesPage::route());
foreach ($I->getContentTypes() as $contentType) {
$I->amGoingTo('check the fields on the ' . $contentType->getHumanName() . ' content type');
$this->testFieldsOnType($I, $contentType);
}
$I->logout();
}
/**
* Test the fields on a specific content type's "manage fields" admin page.
*
* @param AuthenticatedSteps $I
* @param ContentType $contentType
* The ContentType object that should be tested.
*/
protected function testFieldsOnType(AuthenticatedSteps $I, ContentType $contentType)
{
$I->amOnPage(AdminManageFieldsPage::route($contentType->getMachineName()));
foreach ($contentType->getFields() as $field) {
$I->expectTo('see all details in the correct place for the ' . $field->getLabel() . ' field');
$I->see($field->getMachine(), AdminManageFieldsPage::getMachineNameSelector($field->getMachine()));
$I->see($field->getLabel(), AdminManageFieldsPage::getHumanNameSelector($field->getMachine()));
$I->see($field->getType(), AdminManageFieldsPage::getFieldTypeSelector($field->getMachine()));
// Some fields have a blank column for "widget". The field objects know if they should have something in
// this column or not, so we only check that column if there's supposed to be something there.
if ($field->hasWidget()) {
$I->see($field->getWidget()->getName(), AdminManageFieldsPage::getWidgetSelector($field->getMachine()));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment