CommentFieldsTest.php 5.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentFieldsTest.
 */

namespace Drupal\comment\Tests;

10
11
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;

12
13
14
15
/**
 * Tests fields on comments.
 */
class CommentFieldsTest extends CommentTestBase {
16
17
18
19
20
21
22
23

  /**
   * Enable the field UI.
   *
   * @var array
   */
  public static $modules = array('field_ui');

24
25
26
27
28
29
30
31
32
33
34
35
36
  public static function getInfo() {
    return array(
      'name' => 'Comment fields',
      'description' => 'Tests fields on comments.',
      'group' => 'Comment',
    );
  }

  /**
   * Tests that the default 'comment_body' field is correctly added.
   */
  function testCommentDefaultFields() {
    // Do not make assumptions on default node types created by the test
37
    // installation profile, and create our own.
38
    $this->drupalCreateContentType(array('type' => 'test_node_type'));
39
    $this->container->get('comment.manager')->addDefaultField('node', 'test_node_type');
40

41
42
43
    // Check that the 'comment_body' field is present on the comment bundle.
    $instance = $this->container->get('field.info')->getInstance('comment', 'node__comment', 'comment_body');
    $this->assertTrue(!empty($instance), 'The comment_body field is added when a comment bundle is created');
44

45
    $instance->delete();
46
47

    // Check that the 'comment_body' field is deleted.
48
    $field = $this->container->get('field.info')->getField('comment', 'comment_body');
49
    $this->assertTrue(empty($field), 'The comment_body field was deleted');
50
51
52
53

    // Create a new content type.
    $type_name = 'test_node_type_2';
    $this->drupalCreateContentType(array('type' => $type_name));
54
    $this->container->get('comment.manager')->addDefaultField('node', $type_name);
55
56
57

    // Check that the 'comment_body' field exists and has an instance on the
    // new comment bundle.
58
    $field = $this->container->get('field.info')->getField('comment', 'comment_body');
59
    $this->assertTrue($field, 'The comment_body field exists');
60
61
    $instances = $this->container->get('field.info')->getInstances('comment');
    $this->assertTrue(isset($instances['node__comment']['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
62

63
64
    // Test adding a field that defaults to CommentItemInterface::CLOSED.
    $this->container->get('comment.manager')->addDefaultField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED);
65
    $field = entity_load('field_instance_config', 'node.test_node_type.who_likes_ponies');
66
    $this->assertEqual($field->default_value[0]['status'], CommentItemInterface::CLOSED);
67
68
69
  }

  /**
70
   * Tests that comment module works when installed after a content module.
71
   */
72
  function testCommentInstallAfterContentModule() {
73
74
75
76
    // Create a user to do module administration.
    $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
    $this->drupalLogin($this->admin_user);

77
    // Drop default comment field added in CommentTestBase::setup().
78
    entity_load('field_config', 'node.comment')->delete();
79
80
81
82
83
84
85
86
87
88
89
    if ($field = $this->container->get('field.info')->getField('node', 'comment_node_forum')) {
      $field->delete();
    }

    // Purge field data now to allow comment module to be uninstalled once the
    // field has been deleted.
    field_purge_batch(10);
    // Call again as field_purge_batch() won't remove both the instances and
    // field in a single pass.
    field_purge_batch(10);

90
91
    // Disable the comment module.
    $edit = array();
92
93
94
    $edit['uninstall[comment]'] = TRUE;
    $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
    $this->drupalPostForm(NULL, array(), t('Uninstall'));
95
    $this->rebuildContainer();
96
    $this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.');
97

98
    // Enable core content type module (book).
99
100
    $edit = array();
    $edit['modules[Core][book][enable]'] = 'book';
101
    $this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
102

103
    // Now install the comment module.
104
105
    $edit = array();
    $edit['modules[Core][comment][enable]'] = 'comment';
106
    $this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
107
    $this->rebuildContainer();
108
    $this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.');
109
110

    // Create nodes of each type.
111
    $this->container->get('comment.manager')->addDefaultField('node', 'book');
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    $book_node = $this->drupalCreateNode(array('type' => 'book'));

    $this->drupalLogout();

    // Try to post a comment on each node. A failure will be triggered if the
    // comment body is missing on one of these forms, due to postComment()
    // asserting that the body is actually posted correctly.
    $this->web_user = $this->drupalCreateUser(array('access content', 'access comments', 'post comments', 'skip comment approval'));
    $this->drupalLogin($this->web_user);
    $this->postComment($book_node, $this->randomName(), $this->randomName());
  }

  /**
   * Tests that comment module works correctly with plain text format.
   */
  function testCommentFormat() {
    // Disable text processing for comments.
    $this->drupalLogin($this->admin_user);
    $edit = array('instance[settings][text_processing]' => 0);
131
    $this->drupalPostForm('admin/structure/comments/manage/node__comment/fields/comment.node__comment.comment_body', $edit, t('Save settings'));
132

133
134
135
136
137
138
    // Change formatter settings.
    $this->drupalGet('admin/structure/comments/manage/node__comment/display');
    $edit = array('fields[comment_body][type]' => 'text_trimmed', 'refresh_rows' => 'comment_body');
    $commands = $this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh')));
    $this->assertTrue($commands, 'Ajax commands returned');

139
140
    // Post a comment without an explicit subject.
    $this->drupalLogin($this->web_user);
141
    $edit = array('comment_body[0][value]' => $this->randomName(8));
142
    $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Save'));
143
144
  }
}