<?php

/**
 * @file
 * Tests for Configuration Management: Vocabularies.
 */

use Drupal\configuration\Config\ConfigurationManagement;

/**
 * Base class for functional tests for configuration management.
 */
class ConfigurationHandlerVariableTestCase extends ConfigurationWebTestCase {

  protected $profile = 'minimal';

  /**
   * Test info.
   */
  public static function getInfo() {
    return array(
      'name' => t('Handler: Vocabularies'),
      'description' => t('Test the configuration API for vocabularies configurations'),
      'group' => t('Configuration'),
    );
  }

  /**
   * Implementation of DrupalWebTestCase::setUp().
   */
  public function setUp($modules = array()) {
    global $base_url;

    if (empty($modules)) {
      parent::setUp(array(
        'taxonomy',
        'configuration',
      ));
    }
    else {
      parent::setUp($modules);
    }
  }

  public function testImportToActiveStore() {
    // Change the path from where the configurations are loaded.
    $source = drupal_get_path('module', 'configuration') . '/tests/test_configs/';

    $config = 'vocabulary.tags';
    $results = ConfigurationManagement::importToActiveStore(array($config), FALSE, FALSE, FALSE, $source);
    $imported = $results->getInfo('imported');
    $this->assertTrue(in_array($config, $imported), "Configuration for $config was imported.");

    $vocabulary_exists = db_query('SELECT 1 FROM {taxonomy_vocabulary} WHERE machine_name = :machine_name', array(':machine_name' => 'tags'))->fetchField();
    $this->assertTrue($vocabulary_exists, "The vocabulary was saved in the ActiveStore.");
  }

  public function testExportToDataStore() {
    $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    $vocabulary->machine_name = drupal_strtolower($this->randomName());
    $vocabulary->help = '';
    $vocabulary->nodes = array();
    $vocabulary->weight = mt_rand(0, 10);
    taxonomy_vocabulary_save($vocabulary);

    $vocabulary_exists = db_query('SELECT 1 FROM {taxonomy_vocabulary} WHERE machine_name = :machine_name', array(':machine_name' => $vocabulary->machine_name))->fetchField();
    $this->assertTrue($vocabulary_exists, "The vocabulary was saved in the ActiveStore.");

    $config = 'vocabulary.' . $vocabulary->machine_name;
    $results = ConfigurationManagement::exportToDataStore(array($config), FALSE, FALSE);
    $exported = $results->getInfo('exported');
    $this->assertTrue(in_array($config, $exported), "Configuration for $config was exported.");

    $file_for_config = $this->datastore_path . '/' . $config . '.inc';

    $this->assertTrue(file_exists($file_for_config), "The file that storages the vocabulary was created.");
  }

  public function testCheckModifications() {
    // Change the path from where the configurations are loaded.
    $source = drupal_get_path('module', 'configuration') . '/tests/test_configs/';

    $config = 'vocabulary.tags';
    $results = ConfigurationManagement::importToActiveStore(array($config), FALSE, FALSE, TRUE, $source);

    $object = db_select('configuration_tracked', 'ct')
                        ->fields('ct')
                        ->condition('component', 'vocabulary')
                        ->condition('identifier', 'tags')
                        ->execute()
                        ->fetchObject();
    $vid = db_query('SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name = :machine_name', array(':machine_name' => 'tags'))->fetchField();
    $original_vocabulary = taxonomy_vocabulary_load($vid);

    $this->assertTrue(!empty($object), 'The vocabulary was suscessfully tracked.');

    $web_user = $this->drupalCreateUser(
      array(
        'administer taxonomy',
      )
    );

    $this->drupalLogin($web_user);

    $edit = array();
    $edit['name'] = 'Modified';
    $this->drupalPost('admin/structure/taxonomy/tags/edit', $edit, t('Save'));

    $this->assertRaw(t('Updated vocabulary %name.', array('%name' => 'Modified')), 'The vocabulary for site name was modified');

    $results = ConfigurationManagement::importToActiveStore(array('vocabulary.tags'), FALSE, FALSE);
    $current_vocabulary = taxonomy_vocabulary_load($vid);

    $this->assertEqual($original_vocabulary->name, $current_vocabulary->name, 'The vocabulary was suscessfully reverted.');
  }

}
