annotate core/modules/comment/tests/src/Kernel/CommentBundlesTest.php @ 19:fa3358dc1485 tip

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