Newer
Older
catch
committed
<?php
/**
* @file
* Definition of Drupal\comment\Tests\CommentFieldsTest.
*/
namespace Drupal\comment\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldInstanceConfig;
catch
committed
/**
* Tests fields on comments.
*/
class CommentFieldsTest extends CommentTestBase {
/**
* Enable the field UI.
*
* @var array
*/
public static $modules = array('field_ui');
catch
committed
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
Jennifer Hodgdon
committed
// installation profile, and create our own.
catch
committed
$this->drupalCreateContentType(array('type' => 'test_node_type'));
$this->container->get('comment.manager')->addDefaultField('node', 'test_node_type');
catch
committed
// Check that the 'comment_body' field is present on the comment bundle.
$instance = FieldInstanceConfig::loadByName('comment', 'comment', 'comment_body');
$this->assertTrue(!empty($instance), 'The comment_body field is added when a comment bundle is created');
catch
committed
$instance->delete();
catch
committed
// Check that the 'comment_body' field is deleted.
$field = FieldConfig::loadByName('comment', 'comment_body');
Jennifer Hodgdon
committed
$this->assertTrue(empty($field), 'The comment_body field was deleted');
catch
committed
// Create a new content type.
$type_name = 'test_node_type_2';
$this->drupalCreateContentType(array('type' => $type_name));
$this->container->get('comment.manager')->addDefaultField('node', $type_name);
catch
committed
// Check that the 'comment_body' field exists and has an instance on the
// new comment bundle.
$field = FieldConfig::loadByName('comment', 'comment_body');
Jennifer Hodgdon
committed
$this->assertTrue($field, 'The comment_body field exists');
$instance = FieldInstanceConfig::loadByName('comment', 'comment', 'comment_body');
$this->assertTrue(isset($instance), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
Alex Pott
committed
// Test adding a field that defaults to CommentItemInterface::CLOSED.
$this->container->get('comment.manager')->addDefaultField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
$field = entity_load('field_instance_config', 'node.test_node_type.who_likes_ponies');
$this->assertEqual($field->default_value[0]['status'], CommentItemInterface::CLOSED);
catch
committed
}
/**
Dries Buytaert
committed
* Tests that comment module works when installed after a content module.
catch
committed
*/
Dries Buytaert
committed
function testCommentInstallAfterContentModule() {
catch
committed
// Create a user to do module administration.
$this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
$this->drupalLogin($this->admin_user);
// Drop default comment field added in CommentTestBase::setup().
FieldConfig::loadByName('node', 'comment')->delete();
if ($field = FieldConfig::loadByName('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);
catch
committed
// Disable the comment module.
$edit = array();
Dries Buytaert
committed
$edit['uninstall[comment]'] = TRUE;
$this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
$this->drupalPostForm(NULL, array(), t('Uninstall'));
$this->rebuildContainer();
Dries Buytaert
committed
$this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.');
catch
committed
// Enable core content type module (book).
catch
committed
$edit = array();
$edit['modules[Core][book][enable]'] = 'book';
$this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
catch
committed
Dries Buytaert
committed
// Now install the comment module.
catch
committed
$edit = array();
$edit['modules[Core][comment][enable]'] = 'comment';
$this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
$this->rebuildContainer();
Dries Buytaert
committed
$this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.');
catch
committed
// Create nodes of each type.
$this->container->get('comment.manager')->addDefaultField('node', 'book');
catch
committed
$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);
$this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body', $edit, t('Save settings'));
catch
committed
Angie Byron
committed
// Change formatter settings.
$this->drupalGet('admin/structure/comment/manage/comment/display');
Angie Byron
committed
$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');
catch
committed
// Post a comment without an explicit subject.
$this->drupalLogin($this->web_user);
Angie Byron
committed
$edit = array('comment_body[0][value]' => $this->randomName(8));
$this->drupalPostForm('node/' . $this->node->id(), $edit, t('Save'));