View source
<?php
class PMPAPIPushWebTestCase extends DrupalWebTestCase {
protected $privileged_user;
protected $files = array();
const SLEEP_TIME = 1;
public static function getInfo() {
return array(
'name' => 'PMPAPI Push Tests',
'description' => 'Ensure that the basic PMPAPI push functionality functions properly.',
'group' => 'PMPAPI',
);
}
public function setUp() {
parent::setUp(array(
'pmpapi_push',
'file_entity',
));
$this->privileged_user = $this
->drupalCreateUser(array(
'administer PMP API',
));
$this
->drupalLogin($this->privileged_user);
module_load_include('php', 'pmpapi', 'tests/settings');
$vars = pmpapi_test_get_secret_vars();
variable_set('pmpapi_auth_client_id', $vars['client_id']);
variable_set('pmpapi_auth_client_secret', $vars['client_secret']);
variable_set('pmpapi_base_url', $vars['host']);
variable_set('pmpapi_cache', FALSE);
$this
->assertTrue(variable_get('pmpapi_push_push_active'), 'Push is active.');
$this
->setUpFiles();
$this->nodetype = 'testnode';
$node_settings = array(
'entity_type' => 'node',
'bundle_name' => $this->nodetype,
'target_profile' => 'story',
'mapping' => array(
'title' => 'title',
'body' => 'contentencoded',
'field_image' => 'item-image',
),
);
$this
->setUpEntityMapping($node_settings);
$image_settings = array(
'entity_type' => 'file',
'bundle_name' => 'image',
'target_profile' => 'image',
'mapping' => array(
'filename' => 'title',
),
);
$this
->setUpEntityMapping($image_settings);
}
protected function setUpEntityMapping($settings) {
$uname = $settings['entity_type'] . '__' . $settings['bundle_name'];
variable_set('pmpapi_push_' . $uname . '_profile', $settings['target_profile']);
variable_set('pmpapi_push_mapping_' . $uname . '_' . $settings['target_profile'], $settings['mapping']);
}
protected function setUpFiles() {
$types = array(
'text',
'image',
);
foreach ($types as $type) {
foreach ($this
->drupalGetTestFiles($type) as $file) {
$this->files[$type][] = file_save($file);
}
}
}
public function testPmpAPIPushPushNode() {
$this
->createContentType();
$node = $this
->createNode();
$this
->assertTrue($node, 'Created test node.');
$guid = $node->pmpapi_guid;
sleep(self::SLEEP_TIME);
$pmp = pmpapi_fetch($guid);
$this
->assertTrue(empty($pmp->errors['query']), 'Pushed node to PMP.');
sleep(self::SLEEP_TIME);
node_delete($node->nid);
sleep(self::SLEEP_TIME);
$deleted_node = node_load($node->nid);
$this
->assertTrue(!$deleted_node, 'Node deleted from local.');
sleep(self::SLEEP_TIME);
$pmp = pmpapi_fetch($guid);
$this
->assertTrue(!empty($pmp->errors['query']), 'Node deleted from PMP.');
}
public function testPmpAPIPushPushImage() {
$file_entity = $this
->createFileEntity(array(
'type' => 'image',
));
$guid = $file_entity->pmpapi_guid;
sleep(self::SLEEP_TIME);
$pmp = pmpapi_fetch($guid);
$this
->assertTrue(empty($pmp->errors['query']), 'Pushed image to PMP.');
file_delete($file_entity);
sleep(self::SLEEP_TIME);
$pmp = pmpapi_fetch($guid);
$this
->assertTrue(!empty($pmp->errors['query']), 'Image deleted from PMP.');
}
function testPmpAPIPushNodeWithRelatedImage() {
$file_entity = $this
->createFileEntity();
$field_image = array(
'field_name' => 'field_image',
);
$this
->createContentType(array(
$field_image,
));
$settings = array(
'field_image' => array(
LANGUAGE_NONE => array(
0 => array(
'fid' => $file_entity->fid,
'display' => 1,
),
),
),
);
$node = $this
->createNode($settings);
$this
->assertTrue($node, 'Node dumped.');
sleep(self::SLEEP_TIME);
$this
->assertTrue(pmpapi_fetch($file_entity->pmpapi_guid), 'Pushed image to PMP.');
$pmp = pmpapi_fetch($node->pmpapi_guid);
$this
->assertTrue($pmp, 'Pushed node to PMP.');
}
function testPmpAPIPushQueueDelete() {
$this
->createContentType();
$node = $this
->createNode();
$this
->assertTrue($node, 'Created test node.');
sleep(self::SLEEP_TIME);
$guid = $node->pmpapi_guid;
$pmp = pmpapi_fetch($guid);
$this
->assertTrue(empty($pmp->errors['query']), 'Pushed node to PMP.');
$secret = variable_get('pmpapi_auth_client_secret');
variable_set('pmpapi_auth_client_secret', '1234');
DrupalWebTestCase::refreshVariables();
sleep(self::SLEEP_TIME);
node_delete($node->nid);
$this
->assertTrue(!node_load($node->nid), 'Node was deleted.');
sleep(self::SLEEP_TIME);
variable_set('pmpapi_auth_client_secret', $secret);
DrupalWebTestCase::refreshVariables();
$pmp = pmpapi_fetch($guid);
sleep(self::SLEEP_TIME);
$this
->cronRun();
sleep(self::SLEEP_TIME);
$pmp = pmpapi_fetch($guid);
$this
->assertTrue(!empty($pmp->errors), 'Node was deleted on cron run.');
}
function testPmpAPIPushStopDeleteAttempt() {
$file_entity = $this
->createFileEntity();
$field_image = array(
'field_name' => 'field_image',
);
$this
->createContentType(array(
$field_image,
));
$settings = array(
'field_image' => array(
LANGUAGE_NONE => array(
0 => array(
'fid' => $file_entity->fid,
'display' => 1,
),
),
),
);
$node = $this
->createNode($settings);
$this
->assertTrue($node, 'Node dumped.');
sleep(self::SLEEP_TIME);
$this
->assertTrue(pmpapi_fetch($file_entity->pmpapi_guid), 'Pushed image to PMP.');
$pmp = pmpapi_fetch($node->pmpapi_guid);
$this
->assertTrue($pmp, 'Pushed node to PMP.');
}
protected function createFields($fields) {
foreach ($fields as $field) {
field_create_field($field);
}
}
protected function createContentType($instances = array()) {
$content_type = $this
->drupalCreateContentType(array(
'type' => $this->nodetype,
));
if (!empty($instances)) {
foreach ($instances as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $this->nodetype;
field_create_instance($instance);
}
}
return $content_type;
}
protected function createNode($settings = array()) {
$settings += array(
'title' => 'PMPAPI Simpletest test: ' . date('G:i:s'),
'type' => $this->nodetype,
'body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => $this
->randomName(32),
'format' => filter_default_format(),
),
),
),
);
$node = $this
->drupalCreateNode($settings);
return $node;
}
protected function createFileEntity() {
$file = next($this->files['image']);
$this
->assertTrue(file_save($file), 'Created file.');
return $file;
}
}