Chris@0: getCurrentLanguage();
Chris@0: $url_options = [
Chris@0: 'absolute' => TRUE,
Chris@0: 'language' => $language_interface,
Chris@0: ];
Chris@0:
Chris@0: // Setup vocabulary.
Chris@0: Vocabulary::create([
Chris@0: 'vid' => 'tags',
Chris@0: 'name' => 'Tags',
Chris@0: ])->save();
Chris@0:
Chris@0: // Change the title of the admin user.
Chris@0: $this->adminUser->name->value = 'This is a title with some special & > " stuff.';
Chris@0: $this->adminUser->save();
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0:
Chris@0: // Set comment variables.
Chris@0: $this->setCommentSubject(TRUE);
Chris@0:
Chris@18: // To test hostname token field should be populated.
Chris@18: \Drupal::configFactory()
Chris@18: ->getEditable('comment.settings')
Chris@18: ->set('log_ip_addresses', TRUE)
Chris@18: ->save(TRUE);
Chris@18:
Chris@0: // Create a node and a comment.
Chris@0: $node = $this->drupalCreateNode(['type' => 'article', 'title' => '']);
Chris@0: $parent_comment = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
Chris@0:
Chris@0: // Post a reply to the comment.
Chris@0: $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
Chris@0: $child_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
Chris@0: $comment = Comment::load($child_comment->id());
Chris@0: $comment->setHomepage('http://example.org/');
Chris@0:
Chris@0: // Add HTML to ensure that sanitation of some fields tested directly.
Chris@0: $comment->setSubject('');
Chris@0:
Chris@0: // Generate and test tokens.
Chris@0: $tests = [];
Chris@0: $tests['[comment:cid]'] = $comment->id();
Chris@0: $tests['[comment:hostname]'] = $comment->getHostname();
Chris@0: $tests['[comment:author]'] = Html::escape($comment->getAuthorName());
Chris@0: $tests['[comment:mail]'] = $this->adminUser->getEmail();
Chris@0: $tests['[comment:homepage]'] = UrlHelper::filterBadProtocol($comment->getHomepage());
Chris@0: $tests['[comment:title]'] = Html::escape($comment->getSubject());
Chris@0: $tests['[comment:body]'] = $comment->comment_body->processed;
Chris@0: $tests['[comment:langcode]'] = $comment->language()->getId();
Chris@18: $tests['[comment:url]'] = $comment->toUrl('canonical', $url_options + ['fragment' => 'comment-' . $comment->id()])->toString();
Chris@18: $tests['[comment:edit-url]'] = $comment->toUrl('edit-form', $url_options)->toString();
Chris@0: $tests['[comment:created]'] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', ['langcode' => $language_interface->getId()]);
Chris@0: $tests['[comment:created:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getCreatedTime(), ['langcode' => $language_interface->getId()]);
Chris@0: $tests['[comment:changed:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getChangedTimeAcrossTranslations(), ['langcode' => $language_interface->getId()]);
Chris@0: $tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
Chris@0: $tests['[comment:parent:title]'] = $parent_comment->getSubject();
Chris@0: $tests['[comment:entity]'] = Html::escape($node->getTitle());
Chris@0: // Test node specific tokens.
Chris@0: $tests['[comment:entity:nid]'] = $comment->getCommentedEntityId();
Chris@0: $tests['[comment:entity:title]'] = Html::escape($node->getTitle());
Chris@0: $tests['[comment:author:uid]'] = $comment->getOwnerId();
Chris@0: $tests['[comment:author:name]'] = Html::escape($this->adminUser->getDisplayName());
Chris@0:
Chris@0: $base_bubbleable_metadata = BubbleableMetadata::createFromObject($comment);
Chris@0: $metadata_tests = [];
Chris@0: $metadata_tests['[comment:cid]'] = $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:hostname]'] = $base_bubbleable_metadata;
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $bubbleable_metadata->addCacheableDependency($this->adminUser);
Chris@0: $metadata_tests['[comment:author]'] = $bubbleable_metadata;
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $bubbleable_metadata->addCacheableDependency($this->adminUser);
Chris@0: $metadata_tests['[comment:mail]'] = $bubbleable_metadata;
Chris@0: $metadata_tests['[comment:homepage]'] = $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:title]'] = $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:body]'] = $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:langcode]'] = $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:url]'] = $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:edit-url]'] = $base_bubbleable_metadata;
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:created]'] = $bubbleable_metadata->addCacheTags(['rendered']);
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:created:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:changed:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:parent:cid]'] = $bubbleable_metadata->addCacheTags(['comment:1']);
Chris@0: $metadata_tests['[comment:parent:title]'] = $bubbleable_metadata;
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:entity]'] = $bubbleable_metadata->addCacheTags(['node:2']);
Chris@0: // Test node specific tokens.
Chris@0: $metadata_tests['[comment:entity:nid]'] = $bubbleable_metadata;
Chris@0: $metadata_tests['[comment:entity:title]'] = $bubbleable_metadata;
Chris@0: $bubbleable_metadata = clone $base_bubbleable_metadata;
Chris@0: $metadata_tests['[comment:author:uid]'] = $bubbleable_metadata->addCacheTags(['user:2']);
Chris@0: $metadata_tests['[comment:author:name]'] = $bubbleable_metadata;
Chris@0:
Chris@0: // Test to make sure that we generated something for each token.
Chris@0: $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
Chris@0:
Chris@0: foreach ($tests as $input => $expected) {
Chris@0: $bubbleable_metadata = new BubbleableMetadata();
Chris@0: $output = $token_service->replace($input, ['comment' => $comment], ['langcode' => $language_interface->getId()], $bubbleable_metadata);
Chris@0: $this->assertEqual($output, $expected, new FormattableMarkup('Comment token %token replaced.', ['%token' => $input]));
Chris@0: $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
Chris@0: }
Chris@0:
Chris@0: // Test anonymous comment author.
Chris@0: $author_name = 'This is a random & " > string';
Chris@0: $comment->setOwnerId(0)->setAuthorName($author_name);
Chris@0: $input = '[comment:author]';
Chris@0: $output = $token_service->replace($input, ['comment' => $comment], ['langcode' => $language_interface->getId()]);
Chris@0: $this->assertEqual($output, Html::escape($author_name), format_string('Comment author token %token replaced.', ['%token' => $input]));
Chris@0: // Add comment field to user and term entities.
Chris@0: $this->addDefaultCommentField('user', 'user', 'comment', CommentItemInterface::OPEN, 'comment_user');
Chris@0: $this->addDefaultCommentField('taxonomy_term', 'tags', 'comment', CommentItemInterface::OPEN, 'comment_term');
Chris@0:
Chris@0: // Create a user and a comment.
Chris@0: $user = User::create(['name' => 'alice']);
Chris@12: $user->activate();
Chris@0: $user->save();
Chris@0: $this->postComment($user, 'user body', 'user subject', TRUE);
Chris@0:
Chris@0: // Create a term and a comment.
Chris@0: $term = Term::create([
Chris@0: 'vid' => 'tags',
Chris@0: 'name' => 'term',
Chris@0: ]);
Chris@0: $term->save();
Chris@0: $this->postComment($term, 'term body', 'term subject', TRUE);
Chris@0:
Chris@0: // Load node, user and term again so comment_count gets computed.
Chris@0: $node = Node::load($node->id());
Chris@0: $user = User::load($user->id());
Chris@0: $term = Term::load($term->id());
Chris@0:
Chris@0: // Generate comment tokens for node (it has 2 comments, both new),
Chris@0: // user and term.
Chris@0: $tests = [];
Chris@0: $tests['[entity:comment-count]'] = 2;
Chris@0: $tests['[entity:comment-count-new]'] = 2;
Chris@0: $tests['[node:comment-count]'] = 2;
Chris@0: $tests['[node:comment-count-new]'] = 2;
Chris@0: $tests['[user:comment-count]'] = 1;
Chris@0: $tests['[user:comment-count-new]'] = 1;
Chris@0: $tests['[term:comment-count]'] = 1;
Chris@0: $tests['[term:comment-count-new]'] = 1;
Chris@0:
Chris@0: foreach ($tests as $input => $expected) {
Chris@0: $output = $token_service->replace($input, ['entity' => $node, 'node' => $node, 'user' => $user, 'term' => $term], ['langcode' => $language_interface->getId()]);
Chris@0: $this->assertEqual($output, $expected, format_string('Comment token %token replaced.', ['%token' => $input]));
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: }