annotate core/modules/comment/tests/src/Functional/CommentLinksTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\comment\Functional;
Chris@0 4
Chris@0 5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
Chris@0 6 use Drupal\Core\Language\LanguageInterface;
Chris@0 7 use Drupal\comment\CommentInterface;
Chris@0 8 use Drupal\user\RoleInterface;
Chris@0 9 use Drupal\comment\Entity\Comment;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Basic comment links tests to ensure markup present.
Chris@0 13 *
Chris@0 14 * @group comment
Chris@0 15 */
Chris@0 16 class CommentLinksTest extends CommentTestBase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Comment being tested.
Chris@0 20 *
Chris@0 21 * @var \Drupal\comment\CommentInterface
Chris@0 22 */
Chris@0 23 protected $comment;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Seen comments, array of comment IDs.
Chris@0 27 *
Chris@0 28 * @var array
Chris@0 29 */
Chris@0 30 protected $seen = [];
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Use the main node listing to test rendering on teasers.
Chris@0 34 *
Chris@0 35 * @var array
Chris@0 36 *
Chris@0 37 * @todo Remove this dependency.
Chris@0 38 */
Chris@0 39 public static $modules = ['views'];
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Tests that comment links are output and can be hidden.
Chris@0 43 */
Chris@0 44 public function testCommentLinks() {
Chris@0 45 // Bartik theme alters comment links, so use a different theme.
Chris@0 46 \Drupal::service('theme_handler')->install(['stark']);
Chris@0 47 $this->config('system.theme')
Chris@0 48 ->set('default', 'stark')
Chris@0 49 ->save();
Chris@0 50
Chris@0 51 // Remove additional user permissions from $this->webUser added by setUp(),
Chris@0 52 // since this test is limited to anonymous and authenticated roles only.
Chris@0 53 $roles = $this->webUser->getRoles();
Chris@0 54 entity_delete_multiple('user_role', [reset($roles)]);
Chris@0 55
Chris@0 56 // Create a comment via CRUD API functionality, since
Chris@0 57 // $this->postComment() relies on actual user permissions.
Chris@0 58 $comment = Comment::create([
Chris@0 59 'cid' => NULL,
Chris@0 60 'entity_id' => $this->node->id(),
Chris@0 61 'entity_type' => 'node',
Chris@0 62 'field_name' => 'comment',
Chris@0 63 'pid' => 0,
Chris@0 64 'uid' => 0,
Chris@0 65 'status' => CommentInterface::PUBLISHED,
Chris@0 66 'subject' => $this->randomMachineName(),
Chris@0 67 'hostname' => '127.0.0.1',
Chris@0 68 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Chris@0 69 'comment_body' => [['value' => $this->randomMachineName()]],
Chris@0 70 ]);
Chris@0 71 $comment->save();
Chris@0 72 $this->comment = $comment;
Chris@0 73
Chris@0 74 // Change comment settings.
Chris@0 75 $this->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location');
Chris@0 76 $this->setCommentAnonymous(TRUE);
Chris@0 77 $this->node->comment = CommentItemInterface::OPEN;
Chris@0 78 $this->node->save();
Chris@0 79
Chris@0 80 // Change user permissions.
Chris@0 81 $perms = [
Chris@0 82 'access comments' => 1,
Chris@0 83 'post comments' => 1,
Chris@0 84 'skip comment approval' => 1,
Chris@0 85 'edit own comments' => 1,
Chris@0 86 ];
Chris@0 87 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, $perms);
Chris@0 88
Chris@0 89 $nid = $this->node->id();
Chris@0 90
Chris@0 91 // Assert basic link is output, actual functionality is unit-tested in
Chris@0 92 // \Drupal\comment\Tests\CommentLinkBuilderTest.
Chris@0 93 foreach (['node', "node/$nid"] as $path) {
Chris@0 94 $this->drupalGet($path);
Chris@0 95
Chris@0 96 // In teaser view, a link containing the comment count is always
Chris@0 97 // expected.
Chris@0 98 if ($path == 'node') {
Chris@0 99 $this->assertLink(t('1 comment'));
Chris@0 100 }
Chris@0 101 $this->assertLink('Add new comment');
Chris@0 102 }
Chris@0 103
Chris@0 104 // Change weight to make links go before comment body.
Chris@0 105 entity_get_display('comment', 'comment', 'default')
Chris@0 106 ->setComponent('links', ['weight' => -100])
Chris@0 107 ->save();
Chris@18 108 $this->drupalGet($this->node->toUrl());
Chris@0 109 $element = $this->cssSelect('article.js-comment > div');
Chris@0 110 // Get last child element.
Chris@0 111 $element = end($element);
Chris@0 112 $this->assertIdentical($element->getTagName(), 'div', 'Last element is comment body.');
Chris@0 113
Chris@0 114 // Change weight to make links go after comment body.
Chris@0 115 entity_get_display('comment', 'comment', 'default')
Chris@0 116 ->setComponent('links', ['weight' => 100])
Chris@0 117 ->save();
Chris@18 118 $this->drupalGet($this->node->toUrl());
Chris@0 119 $element = $this->cssSelect('article.js-comment > div');
Chris@0 120 // Get last child element.
Chris@0 121 $element = end($element);
Chris@0 122 $this->assertNotEmpty($element->find('css', 'ul.links'), 'Last element is comment links.');
Chris@0 123
Chris@0 124 // Make sure we can hide node links.
Chris@0 125 entity_get_display('node', $this->node->bundle(), 'default')
Chris@0 126 ->removeComponent('links')
Chris@0 127 ->save();
Chris@18 128 $this->drupalGet($this->node->toUrl());
Chris@0 129 $this->assertNoLink('1 comment');
Chris@0 130 $this->assertNoLink('Add new comment');
Chris@0 131
Chris@0 132 // Visit the full node, make sure there are links for the comment.
Chris@0 133 $this->drupalGet('node/' . $this->node->id());
Chris@0 134 $this->assertText($comment->getSubject());
Chris@0 135 $this->assertLink('Reply');
Chris@0 136
Chris@0 137 // Make sure we can hide comment links.
Chris@0 138 entity_get_display('comment', 'comment', 'default')
Chris@0 139 ->removeComponent('links')
Chris@0 140 ->save();
Chris@0 141 $this->drupalGet('node/' . $this->node->id());
Chris@0 142 $this->assertText($comment->getSubject());
Chris@0 143 $this->assertNoLink('Reply');
Chris@0 144 }
Chris@0 145
Chris@0 146 }