View source
<?php
class PMPAPIPullWebTestCase extends DrupalWebTestCase {
protected $privileged_user;
protected $files = array();
const SLEEP_TIME = 1;
public static function getInfo() {
return array(
'name' => 'PMPAPI Pull Tests',
'description' => 'Ensure that the basic PMPAPI pull functionality functions properly.',
'group' => 'PMPAPI',
);
}
public function setUp() {
parent::setUp(array(
'pmpapi_pull',
'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']);
$this->story_guid = $vars['story_guid'];
$this->story_with_image_guid = $vars['story_with_image_guid'];
$this->image_guid = $vars['image_guid'];
variable_set('pmpapi_cache', FALSE);
$this
->assertTrue(variable_get('pmpapi_pull_pull_active'), 'Pull is active.');
$this->nodetype = 'testnode';
$node_settings = array(
'entity_type' => 'node',
'bundle_name' => $this->nodetype,
'mapped_profile' => 'story',
'mapping' => array(
'title' => 'title',
'contentencoded' => 'body',
'item-image' => 'field_image',
),
);
$this
->setUpEntityMapping($node_settings);
$image_settings = array(
'entity_type' => 'file',
'bundle_name' => 'image',
'mapped_profile' => 'image',
'mapping' => array(
'title' => 'filename',
),
);
$this
->setUpEntityMapping($image_settings);
}
protected function setUpEntityMapping($settings) {
$uname = $settings['entity_type'] . '__' . $settings['bundle_name'];
variable_set('pmpapi_pull_' . $uname . '_profile', $settings['mapped_profile']);
variable_set('pmpapi_pull_mapping_' . $uname . '_' . $settings['mapped_profile'], $settings['mapping']);
}
public function testPmpAPIPullPullStory() {
$this
->createContentType();
$node = pmpapi_pull_pull_doc($this->story_guid);
$this
->assertTrue($node->nid, 'Pulled story and created node.');
}
public function testPmpAPIPullPullImage() {
$image = pmpapi_pull_pull_doc($this->image_guid);
$this
->assertTrue($image->fid, 'Pulled image and create image file entity.');
}
public function testPmpAPIPullPullStoryWithImage() {
$field_image = array(
'field_name' => 'field_image',
);
$this
->createContentType(array(
$field_image,
));
$node = pmpapi_pull_pull_doc($this->story_with_image_guid);
$this
->assertTrue($node->nid, 'Pulled story with image and created node.');
$this
->assertTrue(!empty($node->field_image['und'][0]['fid']), 'Image is attached to node.');
}
public function testPmpAPIPullTryBadDoc() {
$this
->createContentType();
$node = pmpapi_pull_pull_doc('1234');
$this
->assertFalse($node, 'Failed to pull bogus GUID.');
}
public function testPmpAPIPullEmbargoedStory() {
$this
->createContentType();
$valid = new stdClass();
$valid->from = pmpapi_convert_timestamp(strtotime('+ 100 days', time()));
$values = array(
'profile' => 'story',
'attributes' => array(
'valid' => $valid,
'title' => $this
->randomName(20),
),
);
$doc = pmpapi_send($values);
$this
->assertTrue(is_object($doc), 'Successfully pushed a story to the PMP.');
sleep(self::SLEEP_TIME);
$guid = $doc->attributes->guid;
pmpapi_pull_pull_doc($guid);
$node = node_load(1);
$this
->assertTrue(is_object($node), 'Pulled story and created node.');
$this
->assertFalse($node->status, 'Embargoed story is unpublished.');
pmpapi_remove($doc->attributes->guid);
}
public function testPmpAPIPullExpiredStory() {
$this
->createContentType();
$valid = new stdClass();
$valid->from = pmpapi_convert_timestamp(strtotime('200 days ago', REQUEST_TIME));
$valid->to = pmpapi_convert_timestamp(strtotime('100 days ago', REQUEST_TIME));
$values = array(
'profile' => 'story',
'attributes' => array(
'valid' => $valid,
'title' => $this
->randomName(20),
),
);
$doc = pmpapi_send($values);
$this
->assertTrue(is_object($doc), 'Successfully pushed a story to the PMP.');
sleep(self::SLEEP_TIME);
$guid = $doc->attributes->guid;
pmpapi_pull_pull_doc($guid);
$node = node_load(1);
$this
->assertTrue(is_object($node), 'Pulled story and created node.');
$this
->assertFalse($node->status, 'Expired story is unpublished.');
pmpapi_remove($doc->attributes->guid);
}
public function testPmpAPIEmbargoedThenValid() {
variable_set('pmpapi_pull_last_validity_check', time() - 100);
$this
->createContentType();
$valid = new stdClass();
$now = time();
$valid->from = pmpapi_convert_timestamp(strtotime('+30 seconds', $now), $now);
$values = array(
'profile' => 'story',
'attributes' => array(
'valid' => $valid,
'title' => $this
->randomName(20),
),
);
$doc = pmpapi_send($values);
$this
->assertTrue(is_object($doc), 'Successfully pushed a doc to the PMP.');
sleep(self::SLEEP_TIME);
$guid = $doc->attributes->guid;
pmpapi_pull_pull_doc($guid);
$node = node_load(1);
$this
->assertTrue(is_object($node), 'Pulled doc and created node.');
$this
->assertFalse($node->status, 'Embargoed node is unpublished.');
sleep(45);
pmpapi_pull_check_valid_dates();
$valid_node = node_load(1, NULL, TRUE);
$this
->assertTrue($valid_node->status, 'Now-valid node is published.');
pmpapi_remove($doc->attributes->guid);
}
public function testPmpAPIValidThenExpired() {
variable_set('pmpapi_pull_last_validity_check', time() - 100);
$this
->createContentType();
$valid = new stdClass();
$now = time();
$valid->from = pmpapi_convert_timestamp(strtotime('-1 day', $now), $now);
$valid->to = pmpapi_convert_timestamp(strtotime('+30 seconds', $now), $now);
$values = array(
'profile' => 'story',
'attributes' => array(
'valid' => $valid,
'title' => $this
->randomName(20),
),
);
$doc = pmpapi_send($values);
$this
->assertTrue(is_object($doc), 'Successfully pushed a doc to the PMP.');
sleep(self::SLEEP_TIME);
$guid = $doc->attributes->guid;
pmpapi_pull_pull_doc($guid);
$node = node_load(1);
$this
->assertTrue(is_object($node), 'Pulled doc and created node.');
$this
->assertTrue($node->status, 'Valid node is published.');
sleep(45);
pmpapi_pull_check_valid_dates();
$valid_node = node_load(1, NULL, TRUE);
$this
->assertFalse($valid_node->status, 'Now-expired node is unpublished.');
pmpapi_remove($doc->attributes->guid);
}
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;
}
}