summaryrefslogtreecommitdiff
path: root/modules/translation/translation.test
blob: 205736e9c2fd01a4724ee48380f9eccff72fbbd7 (plain)
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
<?php
// $Id$

class TranslationTestCase extends DrupalWebTestCase {
  protected $book;

  function getInfo() {
    return array(
      'name' => t('Translation functionality'),
      'description' => t('Create a page with translation, modify the page outdating translation, and update translation.'),
      'group' => t('Translation')
    );
  }

  function setUp() {
    parent::setUp('locale', 'translation');
  }

  /**
   * Create a page with translation, modify the page outdating translation, and update translation.
   */
  function testContentTranslation() {
    // Setup users.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
    $translator = $this->drupalCreateUser(array('create page content', 'edit own page content', 'translate content'));

    $this->drupalLogin($admin_user);

    // Add languages.
    $this->addLanguage('en');
    $this->addLanguage('es');

    // Set page content type to use multilingual support with translation.
    $this->drupalGet('admin/build/node-type/page');
    $edit = array();
    $edit['language_content_type'] = 2;
    $this->drupalPost('admin/build/node-type/page', $edit, t('Save content type'));
    $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Page')), t('Page content type has been updated.'));

    $this->drupalLogout();
    $this->drupalLogin($translator);

    // Create page in English.
    $node_title = $this->randomName();
    $node_body =  $this->randomName();
    $node = $this->createPage($node_title, $node_body, 'en');

    // Submit translation in Spanish.
    $node_translation_title = $this->randomName();
    $node_translation_body = $this->randomName();
    $node_translation = $this->createTranslation($node->nid, $node_translation_title, $node_translation_body, 'es');

    // Update original and mark translation as outdated.
    $edit = array();
    $edit['body'] = $this->randomName();
    $edit['translation[retranslate]'] = TRUE;
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
    $this->assertRaw(t('Page %title has been updated.', array('%title' => $node_title)), t('Original node updated.'));

    // Check to make sure that interface shows translation as outdated
    $this->drupalGet('node/' . $node->nid . '/translate');
    $this->assertRaw('<span class="marker">' . t('outdated') . '</span>', t('Translation marked as outdated.'));

    // Update translation and mark as updated.
    $edit = array();
    $edit['body'] = $this->randomName();
    $edit['translation[status]'] = FALSE;
    $this->drupalPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
    $this->assertRaw(t('Page %title has been updated.', array('%title' => $node_translation_title)), t('Translated node updated.'));
  }

  /**
   * Install a the specified language if it has not been already. Otherwise make sure that
   * the language is enabled.
   *
   * @param string $language_code The language code the check.
   */
  function addLanguage($language_code) {
    // Check to make sure that language has not already been installed.
    $this->drupalGet('admin/settings/language');

    if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
      // Doesn't have language installed so add it.
      $edit = array();
      $edit['langcode'] = $language_code;
      $this->drupalPost('admin/settings/language/add', $edit, t('Add language'));

      $languages = language_list('language', TRUE); // Make sure we're not using a stale list.
      $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));

      if (array_key_exists($language_code, $languages)) {
        $this->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => $languages[$language_code]->name, '@locale-help' => url('admin/help/locale'))), t('Language has been created.'));
      }
    }
    else {
      // Ensure that it is enabled.
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
      $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));

      $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
    }
  }

  /**
   * Create a page in the specified language.
   *
   * @param string $title Title of page in specified language.
   * @param string $body Body of page in specified language.
   * @param string $language Language code.
   */
  function createPage($title, $body, $language) {
    $edit = array();
    $edit['title'] = $title;
    $edit['body'] = $body;
    $edit['language'] = $language;
    $this->drupalPost('node/add/page', $edit, t('Save'));
    $this->assertRaw(t('Page %title has been created.', array('%title' => $edit['title'])), t('Page created.'));

    // Check to make sure the node was created.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertTrue($node, t('Node found in database.'));

    return $node;
  }

  /**
   * Create a translation for the specified page in the specified language.
   *
   * @param integer $nid Node id of page to create translation for.
   * @param string $title Title of page in specified language.
   * @param string $body Body of page in specified language.
   * @param string $language Language code.
   */
  function createTranslation($nid, $title, $body, $language) {
    $this->drupalGet('node/add/page', array('query' => array('translation' => $nid, 'language' => $language)));

    $edit = array();
    $edit['title'] = $title;
    $edit['body'] = $body;
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertRaw(t('Page %title has been created.', array('%title' => $edit['title'])), t('Translation created.'));

    // Check to make sure that translation was successful.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertTrue($node, t('Node found in database.'));

    return $node;
  }
}