annotate core/modules/comment/tests/src/Kernel/CommentBundlesTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\comment\Kernel;
Chris@0 4
Chris@0 5 use Drupal\comment\Entity\CommentType;
Chris@0 6 use Drupal\KernelTests\KernelTestBase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests that comment bundles behave as expected.
Chris@0 10 *
Chris@0 11 * @group comment
Chris@0 12 */
Chris@0 13 class CommentBundlesTest extends KernelTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * {@inheritdoc}
Chris@0 17 */
Chris@0 18 public static $modules = ['comment', 'node', 'taxonomy', 'user'];
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Entity type ids to use for target_entity_type_id on comment bundles.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 protected $targetEntityTypes;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
Chris@0 29 */
Chris@0 30 protected $entityFieldManager;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * {@inheritdoc}
Chris@0 34 */
Chris@0 35 protected function setUp() {
Chris@0 36 parent::setUp();
Chris@0 37
Chris@0 38 $this->entityFieldManager = $this->container->get('entity_field.manager');
Chris@0 39
Chris@0 40 $this->installEntitySchema('comment');
Chris@0 41
Chris@0 42 // Create multiple comment bundles,
Chris@0 43 // each of which has a different target entity type.
Chris@0 44 $this->targetEntityTypes = [
Chris@0 45 'comment' => 'Comment',
Chris@0 46 'node' => 'Node',
Chris@0 47 'taxonomy_term' => 'Taxonomy Term',
Chris@0 48 ];
Chris@0 49 foreach ($this->targetEntityTypes as $id => $label) {
Chris@0 50 CommentType::create([
Chris@0 51 'id' => 'comment_on_' . $id,
Chris@0 52 'label' => 'Comment on ' . $label,
Chris@0 53 'target_entity_type_id' => $id,
Chris@0 54 ])->save();
Chris@0 55 }
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Test that the entity_id field is set correctly for each comment bundle.
Chris@0 60 */
Chris@0 61 public function testEntityIdField() {
Chris@0 62 $field_definitions = [];
Chris@0 63
Chris@0 64 foreach (array_keys($this->targetEntityTypes) as $id) {
Chris@0 65 $bundle = 'comment_on_' . $id;
Chris@0 66 $field_definitions[$bundle] = $this->entityFieldManager
Chris@0 67 ->getFieldDefinitions('comment', $bundle);
Chris@0 68 }
Chris@0 69 // Test that the value of the entity_id field for each bundle is correct.
Chris@0 70 foreach ($field_definitions as $bundle => $definition) {
Chris@0 71 $entity_type_id = str_replace('comment_on_', '', $bundle);
Chris@0 72 $target_type = $definition['entity_id']->getSetting('target_type');
Chris@0 73 $this->assertEquals($entity_type_id, $target_type);
Chris@0 74
Chris@0 75 // Verify that the target type remains correct
Chris@0 76 // in the deeply-nested object properties.
Chris@0 77 $nested_target_type = $definition['entity_id']->getItemDefinition()->getFieldDefinition()->getSetting('target_type');
Chris@0 78 $this->assertEquals($entity_type_id, $nested_target_type);
Chris@0 79 }
Chris@0 80
Chris@0 81 }
Chris@0 82
Chris@0 83 }