Commit 7d1545b3 authored by larowlan's avatar larowlan

Issue #2927569 by tstoeckler, amateescu: Various tests do not set values for...

Issue #2927569 by tstoeckler, amateescu: Various tests do not set values for required field when creating entities
parent bdba2942
......@@ -71,6 +71,9 @@ public function testCommentItem() {
public function testCommentAuthorName() {
$this->installEntitySchema('comment');
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
// Create some comments.
$comment = Comment::create([
'subject' => 'My comment title',
......@@ -78,6 +81,7 @@ public function testCommentAuthorName() {
'name' => 'entity-test',
'mail' => 'entity@localhost',
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
......@@ -95,6 +99,7 @@ public function testCommentAuthorName() {
'mail' => 'test@example.com',
'homepage' => 'https://example.com',
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
......
......@@ -18,6 +18,13 @@
*/
class CommentLinksTest extends CommentViewsKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['entity_test'];
/**
* Views used by this test.
*
......@@ -25,15 +32,27 @@ class CommentLinksTest extends CommentViewsKernelTestBase {
*/
public static $testViews = ['test_comment'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('entity_test');
}
/**
* Test the comment approve link.
*/
public function testLinkApprove() {
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
// Create an unapproved comment.
$comment = $this->commentStorage->create([
'uid' => $this->adminUser->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'status' => 0,
]);
......@@ -91,8 +110,7 @@ public function testLinkApprove() {
* Test the comment reply link.
*/
public function testLinkReply() {
$this->enableModules(['field', 'entity_test']);
$this->installEntitySchema('entity_test');
$this->enableModules(['field']);
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['field']);
......
......@@ -4,6 +4,7 @@
use Drupal\comment\Entity\Comment;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
......@@ -36,6 +37,7 @@ protected function setUp($import_test_views = TRUE) {
$this->installEntitySchema('user');
$this->installEntitySchema('comment');
$this->installEntitySchema('entity_test');
// Create the anonymous role.
$this->installConfig(['user']);
......@@ -67,12 +69,16 @@ protected function setUp($import_test_views = TRUE) {
]);
$this->adminUser->save();
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
// Create some comments.
$comment = Comment::create([
'subject' => 'My comment title',
'uid' => $this->adminUser->id(),
'name' => $this->adminUser->label(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
......@@ -85,6 +91,7 @@ protected function setUp($import_test_views = TRUE) {
'mail' => 'test@example.com',
'homepage' => 'https://example.com',
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'created' => 123456,
'status' => 1,
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\comment\Kernel\Views;
use Drupal\comment\Entity\Comment;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\user\Entity\User;
use Drupal\Tests\views\Kernel\Handler\FieldFieldAccessTestBase;
......@@ -25,6 +26,7 @@ protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('comment');
$this->installEntitySchema('entity_test');
}
/**
......@@ -36,10 +38,14 @@ public function testCommentFields() {
]);
$user->save();
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
$comment = Comment::create([
'subject' => 'My comment title',
'uid' => $user->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
]);
$comment->save();
......@@ -51,6 +57,7 @@ public function testCommentFields() {
'mail' => 'test@example.com',
'homepage' => 'https://example.com',
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'created' => 123456,
'status' => 1,
......
......@@ -71,60 +71,13 @@ protected function setUp() {
$this->entityTypeManager = $this->container->get('entity_type.manager');
}
/**
* Sets up a bundle entity type for the specified entity type, if needed.
*
* @param string $entity_type_id
* The entity type identifier.
*
* @return string
* The bundle identifier.
*/
protected function setupBundleEntityType($entity_type_id) {
$bundle_id = $entity_type_id;
$bundle_entity_type_id = $this->entityTypeManager->getDefinition($entity_type_id)->getBundleEntityType();
if ($bundle_entity_type_id) {
$bundle_entity_type_definition = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
$entity_type_storage = $this->entityTypeManager->getStorage($bundle_entity_type_id);
$entity_type = $entity_type_storage->create([
$bundle_entity_type_definition->getKey('id') => 'example',
]);
if ($entity_type_id == 'media') {
$entity_type->set('source', 'test');
$entity_type->save();
$source_field = $entity_type->getSource()->createSourceField($entity_type);
$source_field->getFieldStorageDefinition()->save();
$source_field->save();
$entity_type->set('source_configuration', [
'source_field' => $source_field->getName(),
]);
}
$entity_type->save();
$bundle_id = $entity_type->id();
}
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle($entity_type_id, $bundle_id);
$workflow->save();
return $bundle_id;
}
/**
* Tests basic monolingual content moderation through the API.
*
* @dataProvider basicModerationTestCases
*/
public function testBasicModeration($entity_type_id) {
$bundle_id = $this->setupBundleEntityType($entity_type_id);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
$entity = $entity_storage->create([
'title' => 'Test title',
$this->entityTypeManager->getDefinition($entity_type_id)->getKey('bundle') => $bundle_id,
]);
$entity = $this->createEntity($entity_type_id);
if ($entity instanceof EntityPublishedInterface) {
$entity->setUnpublished();
}
......@@ -175,6 +128,7 @@ public function testBasicModeration($entity_type_id) {
$entity->save();
// Revert to the previous (published) revision.
$entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
$previous_revision = $entity_storage->loadRevision(4);
$previous_revision->isDefaultRevision(TRUE);
$previous_revision->setNewRevision(TRUE);
......@@ -225,15 +179,9 @@ public function basicModerationTestCases() {
* @dataProvider basicModerationTestCases
*/
public function testContentModerationStateDataRemoval($entity_type_id) {
$bundle_id = $this->setupBundleEntityType($entity_type_id);
// Test content moderation state deletion.
$entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_storage->create([
'title' => 'Test title',
$this->entityTypeManager->getDefinition($entity_type_id)->getKey('bundle') => $bundle_id,
]);
$entity = $this->createEntity($entity_type_id);
$entity->save();
$entity = $this->reloadEntity($entity);
$entity->delete();
......@@ -242,10 +190,7 @@ public function testContentModerationStateDataRemoval($entity_type_id) {
// Test content moderation state revision deletion.
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity2 */
$entity2 = $entity_storage->create([
'title' => 'Test title',
$this->entityTypeManager->getDefinition($entity_type_id)->getKey('bundle') => $bundle_id,
]);
$entity2 = $this->createEntity($entity_type_id);
$entity2->save();
$revision = clone $entity2;
$revision->isDefaultRevision(FALSE);
......@@ -254,6 +199,7 @@ public function testContentModerationStateDataRemoval($entity_type_id) {
$entity2 = $this->reloadEntity($entity2);
$entity2->setNewRevision(TRUE);
$entity2->save();
$entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
$entity_storage->deleteRevision($revision->getRevisionId());
$content_moderation_state = ContentModerationState::loadFromModeratedEntity($revision);
$this->assertFalse($content_moderation_state);
......@@ -263,15 +209,16 @@ public function testContentModerationStateDataRemoval($entity_type_id) {
// Test content moderation state translation deletion.
if ($this->entityTypeManager->getDefinition($entity_type_id)->isTranslatable()) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity3 */
$entity3 = $entity_storage->create([
'title' => 'Test title',
$this->entityTypeManager->getDefinition($entity_type_id)->getKey('bundle') => $bundle_id,
]);
$entity3 = $this->createEntity($entity_type_id);
$langcode = 'it';
ConfigurableLanguage::createFromLangcode($langcode)
->save();
$entity3->save();
$translation = $entity3->addTranslation($langcode, ['title' => 'Titolo test']);
// Make sure we add values for all of the required fields.
if ($entity_type_id == 'block_content') {
$translation->info = $this->randomString();
}
$translation->save();
$content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity3);
$this->assertTrue($content_moderation_state->hasTranslation($langcode));
......@@ -589,6 +536,63 @@ public function testWorkflowNonConfigBundleDependencies() {
], $workflow->getDependencies());
}
/**
* Creates an entity.
*
* The entity will have required fields populated and the corresponding bundle
* will be enabled for content moderation.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return \Drupal\Core\Entity\ContentEntityInterface
* The created entity.
*/
protected function createEntity($entity_type_id) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$bundle_id = $entity_type_id;
// Set up a bundle entity type for the specified entity type, if needed.
if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
$bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
$bundle_entity_storage = $this->entityTypeManager->getStorage($bundle_entity_type_id);
$bundle_id = 'example';
if (!$bundle_entity_storage->load($bundle_id)) {
$bundle_entity = $bundle_entity_storage->create([
$bundle_entity_type->getKey('id') => 'example',
]);
if ($entity_type_id == 'media') {
$bundle_entity->set('source', 'test');
$bundle_entity->save();
$source_field = $bundle_entity->getSource()->createSourceField($bundle_entity);
$source_field->getFieldStorageDefinition()->save();
$source_field->save();
$bundle_entity->set('source_configuration', [
'source_field' => $source_field->getName(),
]);
}
$bundle_entity->save();
}
}
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle($entity_type_id, $bundle_id);
$workflow->save();
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
$entity = $entity_storage->create([
$entity_type->getKey('label') => 'Test title',
$entity_type->getKey('bundle') => $bundle_id,
]);
// Make sure we add values for all of the required fields.
if ($entity_type_id == 'block_content') {
$entity->info = $this->randomString();
}
return $entity;
}
/**
* Reloads the entity after clearing the static cache.
*
......
......@@ -127,6 +127,7 @@ public function testCreateLink() {
'menu_name' => 'menu_test',
'bundle' => 'menu_link_content',
'link' => [['uri' => 'internal:/']],
'title' => 'Link test',
];
$link = MenuLinkContent::create($options);
$link->save();
......@@ -148,7 +149,12 @@ public function testCreateLink() {
public function testMenuLinkOnEntityDelete() {
$user = User::create(['name' => 'username']);
$user->save();
$menu_link_content = MenuLinkContent::create(['menu_name' => 'menu_test', 'link' => [['uri' => 'entity:user/' . $user->id()]], 'bundle' => 'menu_test']);
$menu_link_content = MenuLinkContent::create([
'title' => 'username profile',
'menu_name' => 'menu_test',
'link' => [['uri' => 'entity:user/' . $user->id()]],
'bundle' => 'menu_test',
]);
$menu_link_content->save();
$menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical');
$this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
......
......@@ -70,7 +70,11 @@ public function testTranslationLinkOnMenuEditForm() {
$this->drupalGet('admin/structure/menu/manage/tools');
$this->assertNoLink(t('Translate'));
$menu_link_content = MenuLinkContent::create(['menu_name' => 'tools', 'link' => ['uri' => 'internal:/admin/structure/menu']]);
$menu_link_content = MenuLinkContent::create([
'menu_name' => 'tools',
'link' => ['uri' => 'internal:/admin/structure/menu'],
'title' => 'Link test',
]);
$menu_link_content->save();
$this->drupalGet('admin/structure/menu/manage/tools');
$this->assertLink(t('Translate'));
......
......@@ -108,6 +108,7 @@ public function testOutboundPathAndRouteProcessing() {
$menu_link_content = MenuLinkContent::create([
'link' => ['uri' => $expectation['uri']],
'menu_name' => 'tools',
'title' => 'Link test',
]);
$menu_link_content->save();
$tree = $menu_tree->load('tools', new MenuTreeParameters());
......@@ -128,6 +129,7 @@ public function testOutboundPathAndRouteProcessing() {
$menu_link_content = MenuLinkContent::create([
'link' => ['uri' => $expectation['uri']],
'menu_name' => 'tools',
'title' => 'Link test',
]);
$menu_link_content->save();
$expected_cacheability = $expected_cacheability->merge($expectation['cacheability']);
......
......@@ -62,9 +62,9 @@ public function testDeleteLinksInMenu() {
\Drupal::entityManager()->getStorage('menu')->create(['id' => 'menu1'])->save();
\Drupal::entityManager()->getStorage('menu')->create(['id' => 'menu2'])->save();
\Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content'])->save();
\Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content'])->save();
\Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu2', 'bundle' => 'menu_link_content'])->save();
\Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
\Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu1', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
\Drupal::entityManager()->getStorage('menu_link_content')->create(['link' => ['uri' => 'internal:/menu_name_test'], 'menu_name' => 'menu2', 'bundle' => 'menu_link_content', 'title' => 'Link test'])->save();
$output = $this->linkTree->load('menu1', new MenuTreeParameters());
$this->assertEqual(count($output), 2);
......
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