123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- /*
- * @file
- * Contains tests for Translation management
- */
- /**
- * Base class for tests.
- */
- class TMGMTBaseTestCase extends DrupalWebTestCase {
- protected $profile = 'testing';
- /**
- * A default translator using the test translator.
- *
- * @var TMGMTTranslator
- */
- protected $default_translator;
- /**
- * List of permissions used by loginAsAdmin().
- *
- * @var array
- */
- protected $admin_permissions = array();
- /**
- * Drupal user object created by loginAsAdmin().
- *
- * @var object
- */
- protected $admin_user = NULL;
- /**
- * List of permissions used by loginAsTranslator().
- *
- * @var array
- */
- protected $translator_permissions = array();
- /**
- * Drupal user object created by loginAsTranslator().
- *
- * @var object
- */
- protected $translator_user = NULL;
- /**
- * Overrides DrupalWebTestCase::setUp()
- */
- function setUp() {
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- $modules = array_merge(array('entity', 'tmgmt', 'tmgmt_test'), $modules);
- parent::setUp($modules);
- $this->default_translator = tmgmt_translator_load('test_translator');
- // Load default admin permissions.
- $this->admin_permissions = array(
- 'administer languages',
- 'access administration pages',
- 'administer content types',
- 'administer tmgmt',
- );
- // Load default translator user permissions.
- $this->translator_permissions = array(
- 'create translation jobs',
- 'submit translation jobs',
- 'accept translation jobs',
- );
- }
- /**
- * Will create a user with admin permissions and log it in.
- *
- * @param array $additional_permissions
- * Additional permissions that will be granted to admin user.
- * @param boolean $reset_permissions
- * Flag to determine if default admin permissions will be replaced by
- * $additional_permissions.
- *
- * @return object
- * Newly created and logged in user object.
- */
- function loginAsAdmin($additional_permissions = array(), $reset_permissions = FALSE) {
- $permissions = $this->admin_permissions;
- if ($reset_permissions) {
- $permissions = $additional_permissions;
- }
- elseif (!empty($additional_permissions)) {
- $permissions = array_merge($permissions, $additional_permissions);
- }
- $this->admin_user = $this->drupalCreateUser($permissions);
- $this->drupalLogin($this->admin_user);
- return $this->admin_user;
- }
- /**
- * Will create a user with translator permissions and log it in.
- *
- * @param array $additional_permissions
- * Additional permissions that will be granted to admin user.
- * @param boolean $reset_permissions
- * Flag to determine if default admin permissions will be replaced by
- * $additional_permissions.
- *
- * @return object
- * Newly created and logged in user object.
- */
- function loginAsTranslator($additional_permissions = array(), $reset_permissions = FALSE) {
- $permissions = $this->translator_permissions;
- if ($reset_permissions) {
- $permissions = $additional_permissions;
- }
- elseif (!empty($additional_permissions)) {
- $permissions = array_merge($permissions, $additional_permissions);
- }
- $this->translator_user = $this->drupalCreateUser($permissions);
- $this->drupalLogin($this->translator_user);
- return $this->translator_user;
- }
- /**
- * Creates, saves and returns a translator.
- *
- * @return TMGMTTranslator
- */
- function createTranslator() {
- $translator = new TMGMTTranslator();
- $translator->name = strtolower($this->randomName());
- $translator->label = $this->randomName();
- $translator->plugin = 'test_translator';
- $translator->settings = array(
- 'key' => $this->randomName(),
- 'another_key' => $this->randomName(),
- );
- $this->assertEqual(SAVED_NEW, $translator->save());
- // Assert that the translator was assigned a tid.
- $this->assertTrue($translator->tid > 0);
- return $translator;
- }
- /**
- * Creates, saves and returns a translation job.
- *
- * @return TMGMTJob
- */
- function createJob($source = 'en', $target = 'de', $uid = 1) {
- $job = tmgmt_job_create($source, $target, $uid);
- $this->assertEqual(SAVED_NEW, $job->save());
- // Assert that the translator was assigned a tid.
- $this->assertTrue($job->tjid > 0);
- return $job;
- }
- /**
- * Sets the proper environment.
- *
- * Currently just adds a new language.
- *
- * @param string $langcode
- * The language code.
- */
- function setEnvironment($langcode) {
- // Add the language.
- locale_add_language($langcode);
- }
- /**
- * Asserts job item language codes.
- *
- * @param TMGMTJobItem $job_item
- * Job item to check.
- * @param string $expected_source_lang
- * Expected source language.
- * @param array $actual_lang_codes
- * Expected existing language codes (translations).
- */
- function assertJobItemLangCodes(TMGMTJobItem $job_item, $expected_source_lang, array $actual_lang_codes) {
- $this->assertEqual($job_item->getSourceLangCode(), $expected_source_lang);
- $existing = $job_item->getExistingLangCodes();
- sort($existing);
- sort($actual_lang_codes);
- $this->assertEqual($existing, $actual_lang_codes);
- }
- /**
- * Adds languages as admin user and switches to a translator user.
- */
- protected function createLanguagesLoginTranslator($permissions = NULL) {
- // Login as admin to be able to set environment variables.
- $this->loginAsAdmin();
- $this->setEnvironment('de');
- $this->setEnvironment('es');
- $this->setEnvironment('el');
- $base_permissions = array(
- 'access administration pages',
- 'create translation jobs',
- 'submit translation jobs',
- );
- $permissions = $permissions ? array_merge($permissions, $base_permissions) : $base_permissions;
- // Login as translator only with limited permissions to run these tests.
- $this->loginAsTranslator($permissions, TRUE);
- }
- }
|