forked from LionsAd/drafty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drafty.test
566 lines (493 loc) · 22 KB
/
drafty.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?php
/**
* @file
* Tests for drafty.module.
*/
/**
* Defines a base class for testing Drafty revision support.
*/
class DraftyWebTestCase extends NodeWebTestCase {
function setUp() {
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'drafty';
parent::setUp($modules);
}
/**
* Make it easy for drafty_enforce to subclass.
*
* @param $entity
* An entity object.
*/
protected function setRevision($entity) {
$entity->revision = TRUE;
}
}
/**
* Test drafty revision support with a single entity.
*/
class DraftyTestCase extends DraftyWebTestCase {
protected $type;
public static function getInfo() {
return array(
'name' => 'Drafty',
'description' => 'Test revision manipulation.',
'group' => 'Drafty',
);
}
/**
* Create a node and check creation of draft and published revisions.
*/
function testDraftRevisions() {
$node = new stdClass();
$node->title = 'Title A';
$node->type = 'article';
$node->status = 1;
$this->setRevision($node);
node_save($node);
// Save the vid for later comparison.
$published_vid = $node->vid;
// Save a new draft.
$node = node_load($node->nid);
$this->assertEqual($node->title, 'Title A');
$node->title = 'Title B';
$this->setRevision($node);
$node->is_draft_revision = TRUE;
node_save($node);
$draft_vid = $node->vid;
// Confirm that the published and draft version IDs differ.
$this->assertNotEqual($published_vid, $draft_vid);
// Confirm that loading the node gets the published revision.
$node = node_load($node->nid);
$this->assertEqual($node->title, 'Title A');
drafty()->publishRevision('node', $node->nid, $draft_vid);
$node = node_load($node->nid);
$this->assertEqual($node->title, 'Title B');
$this->assertNotEqual($node->vid, $draft_vid);
}
}
/**
* Test drafty revision support with field collections.
*/
class DraftyFieldCollectionTest extends DraftyWebTestCase {
function setUp() {
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$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);
// Add a text field to the field collection for more human readable testing.
$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);
}
protected $type;
public static function getInfo() {
return array(
'name' => 'Drafty field collection',
'description' => 'Test revision manipulation with field collections.',
'group' => 'Drafty',
);
}
function testDraftyFieldCollection() {
// Create an initial node with no field collection content.
$node = new stdClass();
$node->title = 'Title A';
$node->type = 'article';
$node->status = 1;
$this->setRevision($node);
node_save($node);
// Save the vid for later comparison.
$published_vid = $node->vid;
// Save a new draft of the node with the field collection field populated.
$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;
// Confirm that the published and draft version IDs differ.
$this->assertNotEqual($published_vid, $draft_vid);
// Confirm that loading the node gets the published revision.
$node = node_load($node->nid);
$this->assertEqual($node->title, 'Title A');
$this->assertEqual($node->field_collection_test, array());
// Now publish the draft.
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);
// Now update the field collection with a new value for the text field.
// Also add a new field collection.
$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);
// Ensure the version ID increments.
$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');
// Ensure the second field collection is not populated in the published
// revision.
$this->assertTrue(!isset($node->field_collection_test[LANGUAGE_NONE][1]));
// Publish the new draft.
$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);
// The published version should also increment the version ID.
$this->assertNotEqual($node->vid, $draft_vid);
$this->assertNotEqual($node->vid, $new_draft_vid);
$this->assertTrue($node->vid > $new_draft_vid);
// Republish the old revision.
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);
// Explicitly load the non-default revision and confirm the field collection
// is marked archived.
$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);
}
}
/**
* Test draft revisions with entity translation.
*/
class DraftyEtTest extends EntityTranslationTestCase {
public static function getInfo() {
return array(
'name' => 'Drafty Entity Translation',
'description' => 'Test revision manipulation with entity translation',
'group' => 'Drafty',
);
}
function setUp() {
parent::setUp('locale', 'entity_translation', 'drafty_enforce', 'drafty_1992010');
$this->login($this->getAdminUser());
$this->addLanguage('en');
$this->addLanguage('es');
$this->configureContentType();
$this->login($this->getTranslatorUser());
}
/**
* Test if field based translation works.
*
* Enable field based translation for basic pages. Add a field with a
* cardinality higher than 1, to test if field_default_extract_form_values()
* is invoked. Create a basic page and translate it.
*/
function testFieldTranslation() {
// Create Basic page in English.
$node_title = $this->randomName();
$node_body = $this->randomName();
$node = $this->createPage($node_title, $node_body, 'en');
$original_version = node_load($node->nid, NULL, TRUE);
// Submit translation in Spanish.
$node_translation_title = $this->randomName();
$node_translation_body = $this->randomName();
$node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
$published_version = node_load($node->nid, NULL, TRUE);
// At this point there should be three versions of the node:
// - the original version with no translation.
// - an unpublished version with a translation.
// - the published version with no translation, identical to the original.
$this->assertTrue(!isset($original_version->body['es']), 'No Spanish translation on the original version');
$this->assertTrue(!isset($published_version->body['es']), 'No Spanish translation on the published version');
// Drafty doesn't allow us to load the draft revision while it's being
// created by design, so find it manually based on the two revisions IDs
// we know about.
$vid = db_select('node_revision')
->fields('node_revision', array('vid'))
->condition('nid', $node->nid)
->condition('vid', $original_version->vid, '>')
->condition('vid', $published_version->vid, '<')
->execute()->fetchField();
$draft_version = node_load($node->nid, $vid);
$this->assertTrue($draft_version->body['es'], 'Spanish translation on the draft version');
// Now explicitly publish the draft.
drafty()->publishRevision('node', $node->nid, $draft_version->vid);
$new_published_version = node_load($node->nid, NULL, TRUE);
$this->assertTrue($draft_version->body['es'], 'Spanish translation on the newly published version');
// Now re-publish the original version, and ensure the translation is gone
// again.
drafty()->publishRevision('node', $node->nid, $original_version->vid);
$re_published_original = node_load($node->nid, NULL, TRUE);
$this->assertTrue(!isset($original_version->body['es']), 'No Spanish translation on the re-published original version');
}
}
/**
* Tests for legacy field replacement.
*/
class DraftyTitleTranslationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Drafty with title translation',
'description' => 'Test replaced field translation.',
'group' => 'Drafty',
);
}
protected function setUp() {
parent::setUp('locale', 'entity_translation', 'title', 'drafty', 'drafty_enforce', 'drafty_1992010');
// Create a power user.
$admin_user = $this->drupalCreateUser(array('administer modules', 'view the administration theme', 'administer languages', 'administer entity translation', 'translate any entity'));
$this->drupalLogin($admin_user);
// Enable a translation language.
$edit = array('langcode' => 'it');
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$this->assertTrue(drupal_multilingual(), t('Italian language installed.'));
// Enable URL language negotiation.
$name = 'language_content[enabled][locale-url]';
$edit = array($name => 1);
$this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
$this->assertFieldByName($name, 1, t('URL language negotiation enabled.'));
// Enable node translation.
$name = 'entity_translation_entity_types[node]';
$edit = array($name => 1);
$this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
$this->assertFieldByName($name, 'node', t('Node translation enabled.'));
// Replace the title field.
$entity_type = 'node';
foreach (title_field_replacement_info($entity_type) as $legacy_field => $info) {
title_field_replacement_toggle($entity_type, 'page', $legacy_field);
$t_args = array('%legacy_field' => $legacy_field);
$this->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], 'page'), t('The %legacy_field field has been correctly replaced.', $t_args));
}
// Ensure static caches do not interfere with API calls.
drupal_static_reset();
}
/**
* Tests title module interaction with draft translation creation.
*/
public function testProgrammaticTranslationWorkflow() {
// Create a node and assign it an original language different from
// the default language.
$langcode = 'it';
$original_values = array(
'title' => $langcode . '_' . $this->randomName(),
);
$node = (object) ($original_values + array(
'type' => 'page',
'status' => 1,
));
entity_translation_get_handler('node', $node)->setOriginalLanguage($langcode);
$node->language = $langcode;
node_save($node);
$original_vid = $node->vid;
$this->assertTrue($this->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
$node = $this->nodeLoad($node->nid);
$this->assertTrue($this->checkFieldValues($node, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');
// Pollute synchronization cache to ensure the expected values are stored
// anyway.
title_entity_sync('node', $node, $langcode);
// Create a translation using the default language.
$translation_langcode = language_default()->language;
$translated_values = array(
'title' => $translation_langcode . '_' . $this->randomName(),
);
foreach ($translated_values as $name => $value) {
$node->{$name} = $value;
}
$translation = array(
'language' => $translation_langcode,
'source' => $langcode,
'uid' => $this->loggedInUser->uid,
'status' => 1,
'translate' => 0,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
entity_translation_get_handler('node', $node)->setTranslation($translation);
$node->is_draft_revision = TRUE;
node_save($node);
$node = $this->nodeLoad($node->nid, $translation_langcode);
$new_vid = $node->vid;
// Drafty doesn't allow us to load the draft revision while it's being
// created by design, so find it manually based on the two revisions IDs
// we know about.
$vid = db_select('node_revision')
->fields('node_revision', array('vid'))
->condition('nid', $node->nid)
->condition('vid', $original_vid, '>')
->condition('vid', $new_vid, '<')
->execute()->fetchField();
$node = node_load($node->nid, $vid, TRUE);
$vids = db_query('SELECT vid FROM {node_revision} WHERE nid = :nid', array(':nid' => $node->nid))->fetchCol();
$this->assertTrue($this->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
$node = $this->nodeLoad($node->nid, $translation_langcode);
// Even though the language is set to that of the translation, no
// translation should happen since it's in a draft revision.
$this->assertFalse($this->checkFieldValues($node, $translated_values, $translation_langcode), 'Field translation for draft revision does not override.');
$this->assertTrue($this->checkFieldValues($node, $original_values, $langcode), 'Replacing field original values correctly preserved.');
drafty()->publishRevision('node', $node->nid, $vid);
$node = $this->nodeLoad($node->nid, $translation_langcode);
$this->assertTrue($this->checkFieldValues($node, $translated_values, $translation_langcode), 'Field translation overrides once draft is published.');
$this->assertTrue($this->checkFieldValues($node, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');
// Delete the translation.
entity_translation_get_handler('node', $node)->removeTranslation($translation_langcode);
node_save($node);
$this->assertTrue($this->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
$node = $this->nodeLoad($node->nid, $langcode);
$this->assertTrue($this->checkFieldValues($node, $original_values, $langcode), 'Replacing field translations correctly deleted.');
// Make the node language neutral.
entity_translation_get_handler('node', $node)->setOriginalLanguage(LANGUAGE_NONE);
foreach ($original_values as $name => $value) {
$field_name = $name . '_field';
$node->{$field_name}[LANGUAGE_NONE] = $node->{$field_name}[$langcode];
$node->{$field_name}[$langcode] = array();
}
node_save($node);
$this->assertTrue($this->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
$node = $this->nodeLoad($node->nid);
$this->assertTrue($this->checkFieldValues($node, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');
// Change the node language to the former translation language.
entity_translation_get_handler('node', $node)->setOriginalLanguage($translation_langcode);
foreach ($original_values as $name => $value) {
$field_name = $name . '_field';
$node->{$field_name}[$translation_langcode] = $node->{$field_name}[LANGUAGE_NONE];
$node->{$field_name}[LANGUAGE_NONE] = array();
}
node_save($node);
$this->assertTrue($this->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
$node = $this->nodeLoad($node->nid, $translation_langcode);
$this->assertTrue($this->checkFieldValues($node, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');
// Make a replacing field untranslatable and change its value.
$field_name = 'title_field';
$field = field_info_field($field_name);
$field['translatable'] = FALSE;
field_update_field($field);
$original_values['title'] = LANGUAGE_NONE . '_' . $this->randomName();
$node->title = $original_values['title'];
node_save($node);
$this->assertTrue($this->checkLegacyValues($node, $original_values), 'Legacy field values correctly stored.');
$node = $this->nodeLoad($node->nid);
$this->assertEqual($node->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['title'], 'Untranslatable replacing field on translatable entity correctly handled.');
}
/**
* Loads a term using the given language as active language.
*/
protected function nodeLoad($nid, $langcode = NULL) {
drupal_static_reset();
title_active_language($langcode);
return node_load($nid, NULL, TRUE);
}
/**
* Returns the drupalPost() $edit array corresponding to the given values.
*/
protected function editValues($values, $langcode) {
$edit = array();
foreach ($values as $name => $value) {
$edit["{$name}_field[{$langcode}][0][value]"] = $value;
}
return $edit;
}
/**
* Checks that the field values and optionally the legacy ones match the given values.
*/
protected function checkFieldValues($entity, $values, $langcode, $legacy_match = TRUE) {
foreach ($values as $name => $value) {
$field_name = $name . '_field';
if (!empty($entity->{$field_name}[$langcode])) {
$field_value = $entity->{$field_name}[$langcode][0]['value'];
}
else {
return FALSE;
}
if ($field_value != $value) {
debug($field_value);
debug($value);
return FALSE;
}
if ($legacy_match !== ($field_value == $entity->{$name})) {
debug($legacy_match);
debug($field_value);
debug($entity->{$name});
return FALSE;
}
}
return TRUE;
}
/**
* Checks that the legacy field values stored in the database match the given values.
*/
protected function checkLegacyValues($node, $values) {
$record = db_query('SELECT * FROM {node} n WHERE n.nid = :nid', array(':nid' => $node->nid))->fetchAssoc();
foreach ($values as $name => $value) {
debug($record[$name]);
debug($value);
if ($record[$name] != $value) {
return FALSE;
}
}
return TRUE;
}
}