trait ContentTypeCreationTrait

Same name in other branches
  1. 9 core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php \Drupal\Tests\node\Traits\ContentTypeCreationTrait
  2. 8.9.x core/modules/simpletest/src/ContentTypeCreationTrait.php \Drupal\simpletest\ContentTypeCreationTrait
  3. 8.9.x core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php \Drupal\Tests\node\Traits\ContentTypeCreationTrait
  4. 10 core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php \Drupal\Tests\node\Traits\ContentTypeCreationTrait

Provides methods to create content type from given values.

This trait is meant to be used only by test classes.

Hierarchy

54 files declare their use of ContentTypeCreationTrait
AddModerationConfigActionTest.php in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
BaseFieldOverrideValidationTest.php in core/tests/Drupal/KernelTests/Core/Entity/BaseFieldOverrideValidationTest.php
BlockExposedFilterAJAXTest.php in core/modules/views/tests/src/FunctionalJavascript/BlockExposedFilterAJAXTest.php
BookSettingsFormTest.php in core/modules/book/tests/src/Kernel/BookSettingsFormTest.php
BrowserTestBase.php in core/tests/Drupal/Tests/BrowserTestBase.php

... See full list

File

core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php, line 16

Namespace

Drupal\Tests\node\Traits
View source
trait ContentTypeCreationTrait {
    
    /**
     * Creates a custom content type based on default settings.
     *
     * @param array $values
     *   An array of settings to change from the defaults.
     *   Example: 'type' => 'foo'.
     *
     * @return \Drupal\node\Entity\NodeType
     *   Created content type.
     */
    protected function createContentType(array $values = []) {
        // Find a non-existent random type name.
        if (!isset($values['type'])) {
            do {
                $id = $this->randomMachineName(8);
            } while (NodeType::load($id));
        }
        else {
            $id = $values['type'];
        }
        $values += [
            'type' => $id,
            'name' => $id,
        ];
        $type = NodeType::create($values);
        $status = $type->save();
        node_add_body_field($type);
        if ($this instanceof TestCase) {
            $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', [
                '%type' => $type->id(),
            ]))
                ->__toString());
        }
        else {
            $this->assertEquals(SAVED_NEW, $status, (new FormattableMarkup('Created content type %type.', [
                '%type' => $type->id(),
            ]))
                ->__toString());
        }
        return $type;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ContentTypeCreationTrait::createContentType protected function Creates a custom content type based on default settings. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.