View source
<?php
namespace Drupal\feeds_comment_processor\Tests;
class ProcessorWebTest extends \FeedsWebTestCase {
public static function getInfo() {
return array(
'name' => 'Tests for comment processor',
'description' => 'Tests that comments can be imported.',
'group' => 'Feeds comment processor',
);
}
public function setUp() {
parent::setUp('comment', 'feeds_comment_processor');
$this
->createImporterConfiguration('Test', 'comment');
$this
->setPlugin('comment', 'FeedsCSVParser');
$this
->setPlugin('comment', 'FeedsCommentProcessor');
$edit = array(
'bundle' => 'comment_node_article',
);
$this
->setSettings('comment', 'FeedsCommentProcessor', $edit);
$this
->addMappings('comment', array(
0 => array(
'source' => 'subject',
'target' => 'subject',
),
1 => array(
'source' => 'guid',
'target' => 'nid_by_guid',
),
2 => array(
'source' => 'body',
'target' => 'comment_body',
'format' => 'plain_text',
),
3 => array(
'source' => 'status',
'target' => 'status',
),
4 => array(
'source' => 'hostname',
'target' => 'hostname',
),
));
$parent = (object) array(
'title' => 'Parent',
'type' => 'article',
);
node_save($parent);
$item = (object) array(
'guid' => 10,
'url' => '',
'entity_type' => 'node',
'entity_id' => $parent->nid,
'feed_nid' => 0,
'id' => 'node_importer',
);
drupal_write_record('feeds_item', $item);
}
public function test() {
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_comment_processor') . '/tests/test.csv';
$nid = $this
->createFeedNode('comment', $url, 'Comment test');
$this
->assertText('Created 1 comment');
$comment = comment_load(1);
$this
->assertEqual(1, $comment->nid);
$this
->assertEqual('test subject', $comment->subject);
$this
->assertEqual('example.com', $comment->hostname);
$this
->assertEqual('test body text', $comment->comment_body[LANGUAGE_NONE][0]['value']);
$this
->assertEqual('plain_text', $comment->comment_body[LANGUAGE_NONE][0]['format']);
}
public function testAuthorize() {
$edit = array(
'name' => 'Poor user',
'mail' => 'poor@example.com',
'pass' => user_password(),
'status' => 1,
);
$account = user_save(drupal_anonymous_user(), $edit);
$this
->addMappings('comment', array(
5 => array(
'source' => 'mail',
'target' => 'user_mail',
),
));
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_comment_processor') . '/tests/test.csv';
$nid = $this
->createFeedNode('comment', $url, 'Comment test');
$this
->assertText('Failed importing 1 comment');
$this
->assertText('User ' . $account->name . ' is not permitted to post comments.');
$this
->assertEqual(0, db_query("SELECT COUNT(*) FROM {comment}")
->fetchField());
user_role_change_permissions(2, array(
'post comments' => TRUE,
));
$this
->drupalPost("node/{$nid}/import", array(), 'Import');
$this
->assertText('Created 1 comment.');
$this
->assertEqual(1, db_query("SELECT COUNT(*) FROM {comment}")
->fetchField());
$comment = comment_load(1);
$this
->assertEqual(0, $comment->status);
}
public function testMappingCid() {
$this
->addMappings('comment', array(
5 => array(
'source' => 'guid',
'target' => 'cid',
),
));
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_comment_processor') . '/tests/test.csv';
$nid = $this
->createFeedNode('comment', $url, 'Comment test');
$this
->assertText('Created 1 comment.');
$this
->assertEqual(1, db_query("SELECT COUNT(*) FROM {comment}")
->fetchField());
$comment = comment_load(10);
$this
->assertEqual(10, $comment->cid);
$this
->assertEqual(1, $comment->nid);
$this
->assertEqual('01/', $comment->thread);
}
public function testMappingByTitle() {
$this
->removeMappings('comment', array(
1 => array(
'source' => 'guid',
'target' => 'nid_by_guid',
),
));
$this
->addMappings('comment', array(
4 => array(
'source' => 'title',
'target' => 'nid_by_title',
),
));
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_comment_processor') . '/tests/test.csv';
$nid = $this
->createFeedNode('comment', $url, 'Comment test');
$this
->assertText('Created 1 comment');
$comment = comment_load(1);
$this
->assertEqual(1, $comment->nid);
$this
->assertEqual('test subject', $comment->subject);
$this
->assertEqual('example.com', $comment->hostname);
$this
->assertEqual('test body text', $comment->comment_body[LANGUAGE_NONE][0]['value']);
$this
->assertEqual('plain_text', $comment->comment_body[LANGUAGE_NONE][0]['format']);
}
public function testParentId() {
$this
->addMappings('comment', array(
5 => array(
'source' => 'guid',
'target' => 'guid',
),
));
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_comment_processor') . '/tests/test.csv';
$nid = $this
->createFeedNode('comment', $url, 'Comment test');
$this
->assertText('Created 1 comment');
$this
->removeMappings('comment', array(
1 => array(
'source' => 'guid',
'target' => 'nid_by_guid',
),
5 => array(
'source' => 'guid',
'target' => 'guid',
),
));
$this
->addMappings('comment', array(
4 => array(
'source' => 'title',
'target' => 'nid_by_title',
),
5 => array(
'source' => 'guid',
'target' => 'pid_by_guid',
),
));
$url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_comment_processor') . '/tests/test2.csv';
$edit = array(
'feeds[FeedsHTTPFetcher][source]' => $url,
);
$this
->drupalPost("node/{$nid}/edit", $edit, 'Save');
$this
->drupalPost("node/{$nid}/import", array(), 'Import');
$this
->assertText('Created 1 comment.');
$comment = comment_load(2);
$this
->assertEqual(1, $comment->pid);
$this
->assertEqual(2, $comment->cid);
$this
->assertEqual(1, $comment->nid);
$this
->assertEqual(1, $comment->status);
$this
->assertEqual('test subject 2', $comment->subject);
$this
->assertEqual('01.00/', $comment->thread);
}
}