View source
<?php
class DraftyFieldCollectionTest extends DraftyWebTestCase {
public static function getInfo() {
return array(
'name' => 'Drafty field collection',
'description' => 'Test revision manipulation with field collections.',
'group' => 'Drafty',
'dependencies' => array(
'field_collection',
),
);
}
function setUp(array $modules = array()) {
$modules[] = 'field_collection';
parent::setUp($modules);
$field = array(
'field_name' => 'field_collection_test',
'type' => 'field_collection',
'cardinality' => 5,
);
field_create_field($field);
$instance = array(
'field_name' => 'field_collection_test',
'entity_type' => 'node',
'bundle' => 'article',
);
field_create_instance($instance);
$field = array(
'field_name' => 'field_fc_title',
'type' => 'text',
);
field_create_field($field);
$instance = array(
'field_name' => 'field_fc_title',
'entity_type' => 'field_collection_item',
'bundle' => 'field_collection_test',
);
field_create_instance($instance);
}
function testDraftyFieldCollection() {
$node = new stdClass();
$node->title = 'Title A';
$node->type = 'article';
$node->status = 1;
$this
->setRevision($node);
node_save($node);
$published_vid = $node->vid;
$node = node_load($node->nid);
$this
->assertEqual($node->title, 'Title A');
$node->title = 'Title B';
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => 'field_collection_test',
));
$field_collection_item->field_fc_title[LANGUAGE_NONE][0]['value'] = 'FC Title A';
$node->field_collection_test[LANGUAGE_NONE][0]['entity'] = $field_collection_item;
$this
->setRevision($node);
$node->is_draft_revision = TRUE;
node_save($node);
$draft_vid = $node->vid;
$this
->assertNotEqual($published_vid, $draft_vid);
$node = node_load($node->nid);
$this
->assertEqual($node->title, 'Title A');
$this
->assertEqual($node->field_collection_test, array());
drafty()
->publishRevision('node', $node->nid, $draft_vid);
$node = node_load($node->nid);
$this
->assertEqual($node->title, 'Title B');
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][0]['value']);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title A');
$this
->assertFalse($fc_item->archived);
$this
->assertNotEqual($node->vid, $draft_vid);
$fc_item->field_fc_title[LANGUAGE_NONE][0]['value'] = 'FC Title B';
$node->field_collection_test[LANGUAGE_NONE][0]['entity'] = $fc_item;
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => 'field_collection_test',
));
$field_collection_item->field_fc_title[LANGUAGE_NONE][0]['value'] = 'FC Title C';
$field_collection_item
->setHostEntity('node', $node);
$node->title = 'Title C';
$this
->setRevision($node);
$node->is_draft_revision = TRUE;
$node->field_collection_test[LANGUAGE_NONE][1]['entity'] = $field_collection_item;
node_save($node);
$this
->assertFieldCollectionArchivedCount(1);
$this
->assertFieldCollectionNotArchivedCount(1);
$new_draft_vid = $node->vid;
$node = node_load($node->nid);
$this
->assertNotEqual($draft_vid, $new_draft_vid);
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][0]['value']);
$this
->assertFalse($fc_item->archived);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title A');
$this
->assertTrue(!isset($node->field_collection_test[LANGUAGE_NONE][1]));
$vid = drafty()
->publishRevision('node', $node->nid, $new_draft_vid);
$node = node_load($node->nid);
$this
->assertEqual($node->title, 'Title C');
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][0]['value']);
$this
->assertFalse($fc_item->archived);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title B');
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][1]['value']);
$this
->assertFalse($fc_item->archived);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title C');
$this
->assertFieldCollectionArchivedCount(0);
$this
->assertFieldCollectionNotArchivedCount(2);
$this
->assertNotEqual($node->vid, $draft_vid);
$this
->assertNotEqual($node->vid, $new_draft_vid);
$this
->assertTrue($node->vid > $new_draft_vid);
drafty()
->publishRevision('node', $node->nid, $draft_vid);
$node = node_load($node->nid);
$this
->assertEqual($node->title, 'Title B');
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][0]['value']);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title A');
$this
->assertFalse($fc_item->archived);
$this
->assertTrue(!isset($node->field_collection_test[LANGUAGE_NONE][1]));
$this
->assertFieldCollectionArchivedCount(1);
$this
->assertFieldCollectionNotArchivedCount(1);
$this
->assertNotEqual($node->vid, $new_draft_vid);
$this
->assertNotEqual($node->vid, $draft_vid);
$node = node_load($node->nid, $new_draft_vid);
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][0]['value']);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title A');
$this
->assertFalse($fc_item->archived);
$fc_item = field_collection_item_load($node->field_collection_test[LANGUAGE_NONE][1]['value']);
$this
->assertEqual($fc_item->field_fc_title[LANGUAGE_NONE][0]['value'], 'FC Title C');
$this
->assertFalse($fc_item->archived);
$this
->assertFieldCollectionArchivedCount(1);
$this
->assertFieldCollectionNotArchivedCount(1);
}
protected function assertFieldCollectionArchivedCount($count) {
$archived_count = db_query('SELECT COUNT(*) FROM {field_collection_item} WHERE archived = 1')
->fetchField();
$this
->assertEqual($archived_count, $count);
}
protected function assertFieldCollectionNotArchivedCount($count) {
$not_archived_count = db_query('SELECT COUNT(*) FROM {field_collection_item} WHERE archived = 0')
->fetchField();
$this
->assertEqual($not_archived_count, $count);
}
}