Newer
Older
catch
committed
<?php
/**
* @file
* Definition of Drupal\comment\Tests\CommentPreviewTest.
*/
namespace Drupal\comment\Tests;
Dries Buytaert
committed
use Drupal\Core\Datetime\DrupalDateTime;
catch
committed
/**
* Tests previewing comments.
*/
class CommentPreviewTest extends CommentTestBase {
/**
Angie Byron
committed
* Modules to enable.
Angie Byron
committed
* @var array
Angie Byron
committed
public static $modules = array('image');
catch
committed
public static function getInfo() {
return array(
'name' => 'Comment preview',
'description' => 'Test comment preview.',
'group' => 'Comment',
);
}
Angie Byron
committed
function setUp() {
parent::setUp();
// Create user picture field.
module_load_install('user');
user_install_picture_field();
// Add the basic_html filter format from the standard install profile.
$filter_format_storage_controller = $this->container->get('entity.manager')->getStorageController('filter_format');
Angie Byron
committed
$filter_format = $filter_format_storage_controller->create(array(
'format' => 'basic_html',
'name' => 'Basic HTML',
'status' => TRUE,
Angie Byron
committed
'roles' => array('authenticated'),
), 'filter_format');
$filter_format->setFilterConfig('filter_html', array(
'module' => 'filter',
'status' => TRUE,
Angie Byron
committed
'settings' => array(
'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h4> <h5> <h6> <p> <span> <img>',
),
));
$filter_format->save();
}
catch
committed
/**
* Tests comment preview.
*/
function testCommentPreview() {
// As admin user, configure comment settings.
$this->drupalLogin($this->admin_user);
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
catch
committed
$this->drupalLogout();
// Login as web user and add a signature and a user picture.
$this->drupalLogin($this->web_user);
Angie Byron
committed
\Drupal::config('user.settings')->set('signatures', 1)->save();
catch
committed
$test_signature = $this->randomName();
$edit['signature[value]'] = '<a href="http://example.com/">' . $test_signature. '</a>';
$image = current($this->drupalGetTestFiles('image'));
Angie Byron
committed
$edit['files[user_picture_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('user/' . $this->web_user->id() . '/edit', $edit, t('Save'));
catch
committed
// As the web user, fill in the comment form and preview the comment.
$edit = array();
$edit['subject'] = $this->randomName(8);
Angie Byron
committed
$edit['comment_body[0][value]'] = $this->randomName(16);
$this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
catch
committed
// Check that the preview is displaying the title and body.
Jennifer Hodgdon
committed
$this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
$this->assertText($edit['subject'], 'Subject displayed.');
Angie Byron
committed
$this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
catch
committed
// Check that the title and body fields are displayed with the correct values.
Jennifer Hodgdon
committed
$this->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
Angie Byron
committed
$this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
catch
committed
// Check that the signature is displaying with the correct text format.
$this->assertLink($test_signature);
// Check that the user picture is displayed.
Dries Buytaert
committed
$this->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
catch
committed
}
/**
* Tests comment edit, preview, and save.
*/
function testCommentEditPreviewSave() {
Angie Byron
committed
$web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval', 'edit own comments'));
catch
committed
$this->drupalLogin($this->admin_user);
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
catch
committed
$edit = array();
Dries Buytaert
committed
$date = new DrupalDateTime('2008-03-02 17:23');
catch
committed
$edit['subject'] = $this->randomName(8);
Angie Byron
committed
$edit['comment_body[0][value]'] = $this->randomName(16);
$edit['name'] = $web_user->getUsername();
Dries Buytaert
committed
$edit['date[date]'] = $date->format('Y-m-d');
$edit['date[time]'] = $date->format('H:i:s');
$raw_date = $date->getTimestamp();
catch
committed
$expected_text_date = format_date($raw_date);
Dries Buytaert
committed
$expected_form_date = $date->format('Y-m-d');
$expected_form_time = $date->format('H:i:s');
Angie Byron
committed
$comment = $this->postComment($this->node, $edit['subject'], $edit['comment_body[0][value]'], TRUE);
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
catch
committed
// Check that the preview is displaying the subject, comment, author and date correctly.
Jennifer Hodgdon
committed
$this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
$this->assertText($edit['subject'], 'Subject displayed.');
Angie Byron
committed
$this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
Jennifer Hodgdon
committed
$this->assertText($edit['name'], 'Author displayed.');
$this->assertText($expected_text_date, 'Date displayed.');
catch
committed
// Check that the subject, comment, author and date fields are displayed with the correct values.
Jennifer Hodgdon
committed
$this->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
Angie Byron
committed
$this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
Jennifer Hodgdon
committed
$this->assertFieldByName('name', $edit['name'], 'Author field displayed.');
Dries Buytaert
committed
$this->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
$this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
catch
committed
// Check that saving a comment produces a success message.
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
Jennifer Hodgdon
committed
$this->assertText(t('Your comment has been posted.'), 'Comment posted.');
catch
committed
// Check that the comment fields are correct after loading the saved comment.
$this->drupalGet('comment/' . $comment->id() . '/edit');
Jennifer Hodgdon
committed
$this->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
Angie Byron
committed
$this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
Jennifer Hodgdon
committed
$this->assertFieldByName('name', $edit['name'], 'Author field displayed.');
Dries Buytaert
committed
$this->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
$this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
catch
committed
// Submit the form using the displayed values.
$displayed = array();
$displayed['subject'] = (string) current($this->xpath("//input[@id='edit-subject']/@value"));
Angie Byron
committed
$displayed['comment_body[0][value]'] = (string) current($this->xpath("//textarea[@id='edit-comment-body-0-value']"));
catch
committed
$displayed['name'] = (string) current($this->xpath("//input[@id='edit-name']/@value"));
Dries Buytaert
committed
$displayed['date[date]'] = (string) current($this->xpath("//input[@id='edit-date-date']/@value"));
$displayed['date[time]'] = (string) current($this->xpath("//input[@id='edit-date-time']/@value"));
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
catch
committed
// Check that the saved comment is still correct.
$comment_loaded = comment_load($comment->id(), TRUE);
$this->assertEqual($comment_loaded->subject->value, $edit['subject'], 'Subject loaded.');
Angie Byron
committed
$this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
$this->assertEqual($comment_loaded->name->value, $edit['name'], 'Name loaded.');
$this->assertEqual($comment_loaded->created->value, $raw_date, 'Date loaded.');
Angie Byron
committed
$this->drupalLogout();
// Check that the date and time of the comment are correct when edited by
// non-admin users.
$user_edit = array();
$expected_created_time = $comment_loaded->created->value;
$this->drupalLogin($web_user);
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
$comment_loaded = comment_load($comment->id(), TRUE);
$this->assertEqual($comment_loaded->created->value, $expected_created_time, 'Expected date and time for comment edited.');
$this->drupalLogout();