Newer
Older
Angie Byron
committed
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
<?php
/**
* @file
* Contains \Drupal\entity\Tests\EntityTranslationWorkflowsTest.
*/
namespace Drupal\translation_entity\Tests;
use Drupal\user\Plugin\Core\Entity\User;
/**
* Tests entity translation workflows.
*/
class EntityTranslationWorkflowsTest extends EntityTranslationTestBase {
/**
* The entity used for testing.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('language', 'translation_entity', 'entity_test');
public static function getInfo() {
return array(
'name' => 'Entity Test translation workflows',
'description' => 'Tests the entity translation workflows for the test entity.',
'group' => 'Entity Translation UI',
);
}
function setUp() {
parent::setUp();
$this->setupEntity();
}
/**
* Overrides \Drupal\translation_entity\Tests\EntityTranslationTestBase::getEditorPermissions().
*/
protected function getEditorPermissions() {
return array('administer entity_test content');
}
/**
* Creates a test entity and translate it.
*/
protected function setupEntity() {
$default_langcode = $this->langcodes[0];
// Create a test entity.
$values = array(
'name' => $this->randomName(),
'user_id' => mt_rand(1, 128),
$this->fieldName => array(array('value' => $this->randomName(16))),
);
$id = $this->createEntity($values, $default_langcode);
$this->entity = entity_load($this->entityType, $id, TRUE);
// Create a translation.
$this->drupalLogin($this->translator);
$add_translation_path = $this->controller->getBasePath($this->entity) . "/translations/add/$default_langcode/{$this->langcodes[2]}";
$this->drupalPost($add_translation_path, array(), t('Save'));
}
/**
* Test simple and editorial translation workflows.
*/
function testWorkflows() {
// Test workflows for the editor.
$expected_status = array('edit' => 200, 'overview' => 403, 'add_translation' => 403, 'edit_translation' => 403);
$this->assertWorkflows($this->editor, $expected_status);
// Test workflows for the translator.
$expected_status = array('edit' => 403, 'overview' => 200, 'add_translation' => 200, 'edit_translation' => 200);
$this->assertWorkflows($this->translator, $expected_status);
// Test workflows for the admin.
$expected_status = array('edit' => 200, 'overview' => 200, 'add_translation' => 200, 'edit_translation' => 200);
$this->assertWorkflows($this->administrator, $expected_status);
// Check that translation permissions governate the associated operations.
Dries Buytaert
committed
$ops = array('create' => t('Add'), 'update' => t('Edit'), 'delete' => t('Delete'));
Angie Byron
committed
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
$translations_path = $this->controller->getBasePath($this->entity) . "/translations";
foreach ($ops as $current_op => $label) {
$user = $this->drupalCreateUser(array($this->getTranslatePermission(), "$current_op entity translations"));
$this->drupalLogin($user);
$this->drupalGet($translations_path);
foreach ($ops as $op => $label) {
if ($op != $current_op) {
$this->assertNoLink($label, format_string('No %op link found.', array('%op' => $label)));
}
else {
$this->assertLink($label, 0, format_string('%op link found.', array('%op' => $label)));
}
}
}
}
/**
* Checks that workflows have the expected behaviors for the given user.
*
* @param \Drupal\user\Plugin\Core\Entity\User $user
* The user to test the workflow behavior against.
* @param array $expected_status
* The an associative array with the operation name as key and the expected
* status as value.
*/
protected function assertWorkflows(User $user, $expected_status) {
$default_langcode = $this->langcodes[0];
$languages = language_list();
$args = array('@user_label' => $user->name);
$this->drupalLogin($user);
// Check whether the user is allowed to access the entity form in edit mode.
$edit_path = $this->controller->getEditPath($this->entity);
$options = array('language' => $languages[$default_langcode]);
$this->drupalGet($edit_path, $options);
$this->assertResponse($expected_status['edit'], format_string('The @user_label has the expected edit access.', $args));
// Check whether the user is allowed to access the translation overview.
$langcode = $this->langcodes[1];
$translations_path = $this->controller->getBasePath($this->entity) . "/translations";
$options = array('language' => $languages[$langcode]);
$this->drupalGet($translations_path, $options);
$this->assertResponse($expected_status['overview'], format_string('The @user_label has the expected translation overview access.', $args));
// Check whether the user is allowed to create a translation.
$add_translation_path = $translations_path . "/add/$default_langcode/$langcode";
if ($expected_status['add_translation'] == 200) {
Dries Buytaert
committed
$this->clickLink('Add');
Angie Byron
committed
$this->assertUrl($add_translation_path, $options, 'The translation overview points to the translation form when creating translations.');
// Check that the translation form does not contain shared elements for
// translators.
if ($expected_status['edit'] == 403) {
$this->assertNoSharedElements();
}
}
else {
$this->drupalGet($add_translation_path, $options);
}
$this->assertResponse($expected_status['add_translation'], format_string('The @user_label has the expected translation creation access.', $args));
// Check whether the user is allowed to edit a translation.
$langcode = $this->langcodes[2];
$edit_translation_path = $translations_path . "/edit/$langcode";
$options = array('language' => $languages[$langcode]);
if ($expected_status['edit_translation'] == 200) {
$this->drupalGet($translations_path, $options);
$editor = $expected_status['edit'] == 200;
if ($editor) {
Dries Buytaert
committed
$this->clickLink('Edit', 2);
Angie Byron
committed
// An editor should be pointed to the entity form in multilingual mode.
$this->assertUrl($edit_path, $options, 'The translation overview points to the edit form for editors when editing translations.');
}
else {
Dries Buytaert
committed
$this->clickLink('Edit');
Angie Byron
committed
// While a translator should be pointed to the translation form.
$this->assertUrl($edit_translation_path, $options, 'The translation overview points to the translation form for translators when editing translations.');
// Check that the translation form does not contain shared elements.
$this->assertNoSharedElements();
}
}
else {
$this->drupalGet($edit_translation_path, $options);
}
$this->assertResponse($expected_status['edit_translation'], format_string('The @user_label has the expected translation creation access.', $args));
}
/**
* Assert that the current page does not contain shared form elements.
*/
protected function assertNoSharedElements() {
$language_none = LANGUAGE_NOT_SPECIFIED;
return $this->assertNoFieldByXPath("//input[@name='field_test_text[$language_none][0][value]']", NULL, 'Shared elements are not available on the translation form.');
}
}