<?php

class MetaTagsTestHelper extends DrupalWebTestCase {
  function setUp(array $modules = array()) {
    $modules[] = 'ctools';
    $modules[] = 'token';
    $modules[] = 'metatag';
    $modules[] = 'metatag_test';
    parent::setUp($modules);
  }
}

class MetaTagsUnitTest extends MetaTagsTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'Meta tag unit tests',
      'description' => 'Test basic meta tag functionality.',
      'group' => 'Meta tags',
    );
  }

  /**
   * Test the metatag_config_load_with_defaults() function.
   */
  public function testConfigLoadDefaults() {
    $defaults = metatag_config_load_with_defaults('test:foo');
    $this->assertEqual($defaults, array(
      'description' => array('value' => 'Test foo description'),
      'title' => array('value' => 'Test altered title'),
      'test:foo' => array('value' => 'foobar'),
      'generator' => array('value' => 'Drupal 7 (http://drupal.org)'),
    ));
  }

  public function testEntitySupport() {
    $test_cases[0] = array('type' => 'node', 'expected' => TRUE);
    $test_cases[1] = array('type' => 'node', 'bundle' => 'article', 'expected' => TRUE);
    $test_cases[2] = array('type' => 'node', 'bundle' => 'page', 'expected' => TRUE);
    $test_cases[3] = array('type' => 'node', 'bundle' => 'invalid-bundle', 'expected' => FALSE);
    $test_cases[4] = array('type' => 'user', 'expected' => TRUE);
    $test_cases[5] = array('type' => 'invalid-entity', 'expected' => FALSE);
    foreach ($test_cases as $test_case) {
      $test_case += array('bundle' => NULL);
      $this->assertMetatagEntitySupportsMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
    }

    variable_set('metatag_test_entity_info_disable', TRUE);
    drupal_static_reset('metatag_entity_supports_metatags');
    entity_info_cache_clear();

    $test_cases[2]['expected'] = FALSE;
    $test_cases[4]['expected'] = FALSE;
    foreach ($test_cases as $test_case) {
      $test_case += array('bundle' => NULL);
      $this->assertMetatagEntitySupportsMetatags($test_case['type'], $test_case['bundle'], $test_case['expected']);
    }
  }

  function assertMetatagEntitySupportsMetatags($type, $bundle, $expected) {
    return $this->assertEqual(
      metatag_entity_supports_metatags($type, $bundle),
      $expected,
      t("metatag_entity_supports_metatags(:type, :bundle) was :expected", array(
        ':type' => var_export($type, TRUE),
        ':bundle' => var_export($bundle, TRUE),
        ':expected' => var_export($expected, TRUE),
      ))
    );
  }
}
