<?php

/**
 * @file
 * Mock module for language layer tests.
 */

/**
 * Implements hook_boot().
 *
 * For testing domain language negotiation, we fake it by setting
 * the HTTP_HOST here
 */
function language_test_boot() {
  if (variable_get('language_test_domain')) {
    $_SERVER['HTTP_HOST'] = variable_get('language_test_domain');
  }
}

/**
 * Implements hook_init().
 */
function language_test_init() {
  language_test_store_language_negotiation();
  if (isset(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode) && isset(drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->method_id)) {
    drupal_set_message(t('Language negotiation method: @name', array('@name' => drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->method_id)));
  }
}

/**
 * Implements hook_language_types_info().
 */
function language_test_language_types_info() {
  if (variable_get('language_test_language_types', FALSE)) {
    return array(
      'test_language_type' => array(
        'name' => t('Test'),
        'description' => t('A test language type.'),
      ),
      'fixed_test_language_type' => array(
        'fixed' => array('test_language_negotiation_method'),
      ),
    );
  }
}

/**
 * Implements hook_language_types_info_alter().
 */
function language_test_language_types_info_alter(array &$language_types) {
  if (variable_get('language_test_content_language_type', FALSE)) {
    unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
  }
}

/**
 * Implements hook_language_negotiation_info().
 */
function language_test_language_negotiation_info() {
  if (variable_get('language_test_language_negotiation_info', FALSE)) {
    $info = array(
      'callbacks' => array(
        'negotiation' => 'language_test_language_negotiation_method',
      ),
      'file' => drupal_get_path('module', 'language_test') .'/language_test.module',
      'weight' => -10,
      'description' => t('This is a test language negotiation method.'),
    );

    return array(
      'test_language_negotiation_method' => array(
        'name' => t('Test'),
        'types' => array(LANGUAGE_TYPE_CONTENT, 'test_language_type', 'fixed_test_language_type'),
      ) + $info,
      'test_language_negotiation_method_ts' => array(
        'name' => t('Type-specific test'),
        'types' => array('test_language_type'),
      ) + $info,
    );
  }
}

/**
 * Implements hook_language_negotiation_info_alter().
 */
function language_test_language_negotiation_info_alter(array &$negotiation_info) {
  if (variable_get('language_test_language_negotiation_info_alter', FALSE)) {
    unset($negotiation_info[LANGUAGE_NEGOTIATION_INTERFACE]);
  }
}

/**
 * Store the last negotiated languages.
 */
function language_test_store_language_negotiation() {
  $last = array();
  foreach (language_types_get_all() as $type) {
    $last[$type] = $GLOBALS[$type]->langcode;
  }
  variable_set('language_test_language_negotiation_last', $last);
}

/**
 * Provides a test language negotiation method.
 */
function language_test_language_negotiation_method($languages) {
  return 'it';
}
