Commit 99507a38 authored by catch's avatar catch

Issue #1919468 by alexpott, Berdir: EntityManager::getAllBundleInfo() adds...

Issue #1919468 by alexpott, Berdir: EntityManager::getAllBundleInfo() adds default entity_type_id bundle for entity types with bundles
parent 07da832d
......@@ -97,18 +97,18 @@ public function getAllBundleInfo() {
}
else {
$this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info');
// First look for entity types that act as bundles for others, load them
// and add them as bundles.
foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
if ($entity_type->getBundleOf()) {
foreach ($this->entityTypeManager->getStorage($type)->loadMultiple() as $entity) {
$this->bundleInfo[$entity_type->getBundleOf()][$entity->id()]['label'] = $entity->label();
// First look for entity types that act as bundles for others, load them
// and add them as bundles.
if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
foreach ($this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple() as $entity) {
$this->bundleInfo[$type][$entity->id()]['label'] = $entity->label();
}
}
}
foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
// If no bundles are provided, use the entity type name and label.
if (!isset($this->bundleInfo[$type])) {
// If entity type bundles are not supported and
// hook_entity_bundle_info() has not already set up bundle
// information, use the entity type name and label.
elseif (!isset($this->bundleInfo[$type])) {
$this->bundleInfo[$type][$type]['label'] = $entity_type->getLabel();
}
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\block_content\Tests\Migrate;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\migrate\MigrateException;
use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase;
use Drupal\migrate_drupal\Tests\StubTestTrait;
......@@ -37,13 +38,15 @@ protected function setUp() {
* Tests creation of block content stubs with no block_content_type available.
*/
public function testStubFailure() {
$entity_id = $this->createStub('block_content');
$violations = $this->validateStub('block_content', $entity_id);
$this->assertIdentical(count($violations), 1);
$this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', [
'%type' => 'block_content_type',
'%id' => 'block_content',
]));
$message = 'Expected MigrateException thrown when no bundles exist.';
try {
$this->createStub('block_content');
$this->fail($message);
}
catch (MigrateException $e) {
$this->pass($message);
$this->assertEqual('Stubbing failed, no bundles available for entity type: block_content', $e->getMessage());
}
}
/**
......
......@@ -19,7 +19,7 @@ class ContentTranslationEnableTest extends WebTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test', 'menu_link_content'];
public static $modules = ['entity_test', 'menu_link_content', 'node'];
/**
* Tests that entity schemas are up-to-date after enabling translation.
......@@ -39,6 +39,9 @@ public function testEnable() {
$requirement_value = $this->cssSelect("tr.system-status-report__entry th:contains('Entity/field definitions') + td");
$this->assertEqual(t('Up to date'), trim((string) $requirement_value[0]));
$this->drupalGet('admin/config/regional/content-language');
// The node entity type should not be an option because it has no bundles.
$this->assertNoRaw('entity_types[node]');
// Enable content translation on entity types that have will have a
// content_translation_uid.
$edit = [
......@@ -47,12 +50,23 @@ public function testEnable() {
'entity_types[entity_test_mul]' => TRUE,
'settings[entity_test_mul][entity_test_mul][translatable]' => TRUE,
];
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
// No pending updates should be available.
$this->drupalGet('admin/reports/status');
$requirement_value = $this->cssSelect("tr.system-status-report__entry th:contains('Entity/field definitions') + td");
$this->assertEqual(t('Up to date'), trim((string) $requirement_value[0]));
// Create a node type and check the content translation settings are now
// available for nodes.
$edit = array(
'name' => 'foo',
'title_label' => 'title for foo',
'type' => 'foo',
);
$this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type'));
$this->drupalGet('admin/config/regional/content-language');
$this->assertRaw('entity_types[node]');
}
}
......@@ -63,7 +63,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$bundles = $this->entityManager->getAllBundleInfo();
$language_configuration = array();
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface || !$entity_type->hasKey('langcode')) {
if (!$entity_type instanceof ContentEntityTypeInterface || !$entity_type->hasKey('langcode') || !isset($bundles[$entity_type_id])) {
continue;
}
$labels[$entity_type_id] = $entity_type->getLabel() ?: $entity_type_id;
......
......@@ -84,7 +84,7 @@ public function testLanguageStringSelector() {
$this->drupalGet($path);
// Get en language from selector.
$elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => 'edit-settings-node-node-settings-language-langcode', ':option' => 'en'));
$elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => 'edit-settings-user-user-settings-language-langcode', ':option' => 'en'));
// Check that the language text is translated.
$this->assertEqual((string) $elements[0], $name_translation, 'Checking the option string English is translated to Spanish.');
......
......@@ -160,6 +160,9 @@ protected function updateEntity(EntityInterface $entity, Row $row) {
protected function processStubRow(Row $row) {
$bundle_key = $this->getKey('bundle');
if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) {
if (empty($this->bundles)) {
throw new MigrateException('Stubbing failed, no bundles available for entity type: ' . $this->storage->getEntityTypeId());
}
$row->setDestinationProperty($bundle_key, reset($this->bundles));
}
......
......@@ -199,7 +199,9 @@ protected function display_options_row(&$display_options, $row_plugin, $row_opt
protected function buildFilters(&$form, FormStateInterface $form_state) {
parent::buildFilters($form, $form_state);
$selected_bundle = static::getSelected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
if (isset($form['displays']['show']['type'])) {
$selected_bundle = static::getSelected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
}
// Add the "tagged with" filter to the view.
......
......@@ -218,6 +218,9 @@ public function testNodeTypeFieldUiPermissions() {
* Tests for when there are no content types defined.
*/
public function testNodeTypeNoContentType() {
/** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
$bundle_info = \Drupal::service('entity_type.bundle.info');
$this->assertEqual(2, count($bundle_info->getBundleInfo('node')), 'The bundle information service has 2 bundles for the Node entity type.');
$web_user = $this->drupalCreateUser(['administer content types']);
$this->drupalLogin($web_user);
......@@ -231,6 +234,9 @@ public function testNodeTypeNoContentType() {
$this->assertRaw(t('No content types available. <a href=":link">Add content type</a>.', [
':link' => Url::fromRoute('node.type_add')->toString()
]), 'Empty text when there are no content types in the system is correct.');
$bundle_info->clearCachedBundles();
$this->assertEqual(0, count($bundle_info->getBundleInfo('node')), 'The bundle information service has 0 bundles for the Node entity type.');
}
}
......@@ -189,7 +189,10 @@ public function testWizardForm() {
$this->drupalPostAjaxForm(NULL, array('show[wizard_key]' => 'users'), 'show[wizard_key]');
$this->assertNoFieldByName('show[type]', NULL, 'The "of type" filter is not added for users.');
$this->drupalPostAjaxForm(NULL, array('show[wizard_key]' => 'node'), 'show[wizard_key]');
$this->assertFieldByName('show[type]', 'all', 'The "of type" filter is added for nodes.');
$this->assertNoFieldByName('show[type]', 'all', 'The "of type" filter is not added for nodes when there are no node types.');
$this->drupalCreateContentType(array('type' => 'page'));
$this->drupalPostAjaxForm(NULL, array('show[wizard_key]' => 'node'), 'show[wizard_key]');
$this->assertFieldByName('show[type]', 'all', 'The "of type" filter is added for nodes when there is at least one node type.');
}
/**
......
......@@ -175,11 +175,11 @@ public function testGetBundleInfo($entity_type_id, $expected) {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()->willReturn('Apple');
$apple->getBundleOf()->willReturn(NULL);
$apple->getBundleEntityType()->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()->willReturn('Banana');
$banana->getBundleOf()->willReturn(NULL);
$banana->getBundleEntityType()->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
......@@ -223,11 +223,11 @@ public function testGetAllBundleInfo() {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()->willReturn('Apple');
$apple->getBundleOf()->willReturn(NULL);
$apple->getBundleEntityType()->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()->willReturn('Banana');
$banana->getBundleOf()->willReturn(NULL);
$banana->getBundleEntityType()->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
......@@ -271,4 +271,49 @@ public function testGetAllBundleInfo() {
$this->assertSame('cached data', $bundle_info);
}
/**
* @covers ::getAllBundleInfo
*/
public function testGetAllBundleInfoWithEntityBundleInfo() {
// Ensure that EntityTypeBundleInfo::getAllBundleInfo() does not add
// additional bundles if hook_entity_bundle_info() defines some and the
// entity_type does not define a bundle entity type.
$this->moduleHandler->invokeAll('entity_bundle_info')->willReturn([
'banana' => [
'fig' => [
'label' => 'Fig banana',
],
],
]);
$this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL);
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()->willReturn('Apple');
$apple->getBundleEntityType()->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()->willReturn('Banana');
$banana->getBundleEntityType()->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$expected = [
'banana' => [
'fig' => [
'label' => 'Fig banana',
],
],
'apple' => [
'apple' => [
'label' => 'Apple',
],
],
];
$bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
$this->assertSame($expected, $bundle_info);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment