You are here

class DataLayerUnitTests in dataLayer 7

@file Tests the functionality of the DataLayer module.

Hierarchy

Expanded class hierarchy of DataLayerUnitTests

File

tests/datalayer.unit.test, line 7
Tests the functionality of the DataLayer module.

View source
class DataLayerUnitTests extends DrupalUnitTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'DataLayer Unit Tests',
      'description' => 'Tests to ensure data makes it client-side.',
      'group' => 'DataLayer',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    drupal_load('module', 'datalayer');
    parent::setUp();
  }

  /**
   * Test DataLayer Defaults function.
   */
  public function testDataLayerDefaults() {
    $this
      ->setupMockLanguage();
    $this
      ->assertEqual(array(
      'drupalLanguage' => 'en-us',
      'drupalCountry' => '',
    ), _datalayer_defaults());
  }

  /**
   * Test DataLayer Add Will Add Data.
   */
  public function testDataLayerAddWillAddData() {
    $this
      ->setupEmptyDataLayer();
    $this
      ->assertEqual(array(
      'foo' => 'bar',
    ), datalayer_add(array(
      'foo' => 'bar',
    )));
  }

  /**
   * Test DataLayer Add Does Not Overwrite By Default.
   */
  public function testDataLayerAddDoesNotOverwriteByDefault() {
    $this
      ->setupEmptyDataLayer();
    datalayer_add(array(
      'foo' => 'bar',
    ));
    $this
      ->assertEqual(array(
      'foo' => 'bar',
    ), datalayer_add(array(
      'foo' => 'baz',
    )));
  }

  /**
   * Test DataLayer Add Will Overwrite With Flag.
   */
  public function testDataLayerAddWillOverwriteWithFlag() {
    $this
      ->setupEmptyDataLayer();
    datalayer_add(array(
      'foo' => 'bar',
    ));
    $this
      ->assertEqual(array(
      'foo' => 'baz',
    ), datalayer_add(array(
      'foo' => 'baz',
    ), TRUE));
  }

  /**
   * Test DataLayer Menu Get Any Object.
   *
   * Returns False Without Load Functions.
   */
  public function testDataLayerMenuGetAnyObjectReturnsFalseWithoutLoadFunctions() {
    $item = $this
      ->setupMockNode();
    $item['node/1']['load_functions'] = NULL;
    $item_static =& drupal_static('menu_get_item');
    $item_static = $item;
    $return_type = FALSE;
    $result = _datalayer_menu_get_any_object($return_type);
    $this
      ->assertEqual($return_type, FALSE);
    $this
      ->assertEqual($result, FALSE);
  }

  /**
   * Test DataLayer Menu Get Any Object.
   *
   * Returns False Without Load Function Match.
   */
  public function testDataLayerMenuGetAnyObjectReturnsFalseWithoutLoadFunctionMatch() {
    $item = $this
      ->setupMockNode();
    $item['node/1']['load_functions'] = array(
      1 => 'user_load',
    );
    $item_static =& drupal_static('menu_get_item');
    $item_static = $item;
    $return_type = FALSE;
    $result = _datalayer_menu_get_any_object($return_type);
    $this
      ->assertEqual($return_type, FALSE);
    $this
      ->assertEqual($result, FALSE);
  }

  /**
   * Test DataLayer Menu Get Any Object.
   *
   * Returns False With Incorrect Arg Position.
   */
  public function testDataLayerMenuGetAnyObjectReturnsFalseWithIncorrectArgPosition() {
    $item = $this
      ->setupMockNode();
    $item['node/1']['load_functions'] = array(
      'user_load',
    );
    $item_static =& drupal_static('menu_get_item');
    $item_static = $item;
    $return_type = FALSE;
    $result = _datalayer_menu_get_any_object($return_type);
    $this
      ->assertEqual($return_type, FALSE);
    $this
      ->assertEqual($result, FALSE);
  }

  /**
   * Test DataLayer Menu Get Any Object Returns Object.
   */
  public function testDataLayerMenuGetAnyObjectReturnsObject() {
    $item = $this
      ->setupMockNode();
    $return_type = FALSE;
    $object = _datalayer_menu_get_any_object($return_type);
    $this
      ->assertEqual($return_type, 'node');
    $this
      ->assertEqual($object, $item['node/1']['map'][1]);
  }

  /**
   * Test DataLayer Get Entity Terms Returns Empty Array.
   */
  public function testDataLayerGetEntityTermsReturnsEmptyArray() {
    $item = $this
      ->setupMockNode();
    $this
      ->setupMockFieldMap();
    $terms = _datalayer_get_entity_terms('node', 'page', $item['node/1']['map'][1]);
    $this
      ->assertEqual(array(), $terms);
  }

  /**
   * Test DataLayer Get Entity Terms Returns Term Array.
   */
  public function testDataLayerGetEntityTermsReturnsTermArray() {
    $item = $this
      ->setupMockNode();
    $this
      ->setupMockEntityTerms();
    $terms = _datalayer_get_entity_terms('node', 'article', $item['node/1']['map'][1]);
    $this
      ->assertEqual(array(
      'tags' => array(
        1 => 'someTag',
      ),
    ), $terms);
  }

  /**
   * Test DataLayer Get Entity Terms Returns Entity Data Array.
   */
  public function testDataLayerGetEntityDataReturnsEntityDataArray() {
    $this
      ->setupEmptyDataLayer();
    $item = $this
      ->setupMockNode();
    $this
      ->setupMockEntityTerms();
    $entity_data = _datalayer_get_entity_data($item['node/1']['map'][1], 'node');
    $this
      ->assertEqual($this
      ->getExpectedEntityDataArray(), $entity_data);
  }

  /**
   * Setup user.
   */
  public function setupMockUser() {
    global $user;
    $user->uid = 1;
  }

  /**
   * Setup language.
   */
  public function setupMockLanguage($lang = 'en-us') {
    global $language;
    $language->language = $lang;
  }

  /**
   * Setup empty datalayer.
   */
  public function setupEmptyDataLayer() {
    $data =& drupal_static('datalayer_add', array());
  }

  /**
   * Setup mock node.
   */
  public function setupMockNode() {

    // Hijack static cache for menu_get_item call.
    $item =& drupal_static('menu_get_item');
    $_GET['q'] = 'node/1';
    $item = array(
      'node/1' => array(
        'load_functions' => array(
          1 => 'node_load',
        ),
        'map' => array(
          'node',
          (object) array(
            'vid' => 1,
            'uid' => 1,
            'title' => 'My Article',
            'status' => 1,
            'nid' => 1,
            'type' => 'article',
            'language' => 'und',
            'created' => '1435019805',
            'changed' => '1435019805',
            'tnid' => 0,
            'name' => 'admin',
            'field_tags' => array(
              'und' => array(
                array(
                  'tid' => 1,
                  'taxonomy_term' => (object) array(
                    'name' => 'someTag',
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );

    // Hijack static cache for entity_get_info call.
    $entity =& drupal_static('entity_get_info');
    $entity = array(
      'node' => array(
        'load hook' => 'node_load',
      ),
    );
    return $item;
  }

  /**
   * Setup Mock Field Map.
   */
  public function setupMockFieldMap() {
    $field_map =& drupal_static('_field_info_field_cache');
    $field_map = new DataLayerMockFieldInfo();
  }

  /**
   * Setup Mock Field Language.
   */
  public function setupMockFieldLanguage() {
    $field_language =& drupal_static('field_language');
    $field_language = array(
      'node' => array(
        1 => array(
          'en' => array(
            'field_tags' => 'und',
          ),
        ),
      ),
    );
  }

  /**
   * Setup Mock Entity Info.
   */
  public function setupMockEntityInfo() {
    $entity_info =& drupal_static('entity_get_info');
    $entity_info = array(
      'node' => array(
        'entity keys' => array(
          'id' => 'nid',
          'revision' => 'vid',
          'bundle' => 'type',
          'label' => 'title',
          'language' => 'language',
        ),
      ),
      'taxonomy_term' => array(
        'controller class' => 'TaxonomyTermController',
        'base table' => 'taxonomy_term_data',
        'uri callback' => 'taxonomy_term_uri',
        'entity keys' => array(
          'id' => 'tid',
          'bundle' => 'vocabulary_machine_name',
          'label' => 'name',
          'revision' => '',
        ),
        'bundles' => array(
          'tags' => array(
            'label' => 'Tags',
            'admin' => array(
              'path' => 'admin/structure/taxonomy/%taxonomy_vocabulary_machine_name',
              'real path' => 'admin/structure/taxonomy/tags',
              'bundle argument' => 3,
              'access arguments' => array(
                0 => 'administer taxonomy',
              ),
            ),
          ),
        ),
      ),
    );
  }

  /**
   * Setup Mock Entity Controller.
   */
  public function setupMockEntityController() {
    $entity_contoller =& drupal_static('entity_get_controller');
    $entity_contoller = array(
      'taxonomy_term' => new DataLayerMockEntityController(),
    );
  }

  /**
   * Setup Mock Entity Terms.
   */
  public function setupMockEntityTerms() {
    $this
      ->setupMockFieldMap();
    $this
      ->setupMockLanguage('en');
    $this
      ->setupMockFieldLanguage();
    $this
      ->setupMockEntityInfo();
    $this
      ->setupMockEntityController();
  }

  /**
   * Get expected entity data array.
   */
  public function getExpectedEntityDataArray() {
    return array(
      'entityType' => 'node',
      'entityBundle' => 'article',
      'entityId' => 1,
      'entityLabel' => 'My Article',
      'entityTaxonomy' => array(
        'tags' => array(
          1 => 'someTag',
        ),
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DataLayerUnitTests::getExpectedEntityDataArray public function Get expected entity data array.
DataLayerUnitTests::getInfo public static function
DataLayerUnitTests::setUp public function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
DataLayerUnitTests::setupEmptyDataLayer public function Setup empty datalayer.
DataLayerUnitTests::setupMockEntityController public function Setup Mock Entity Controller.
DataLayerUnitTests::setupMockEntityInfo public function Setup Mock Entity Info.
DataLayerUnitTests::setupMockEntityTerms public function Setup Mock Entity Terms.
DataLayerUnitTests::setupMockFieldLanguage public function Setup Mock Field Language.
DataLayerUnitTests::setupMockFieldMap public function Setup Mock Field Map.
DataLayerUnitTests::setupMockLanguage public function Setup language.
DataLayerUnitTests::setupMockNode public function Setup mock node.
DataLayerUnitTests::setupMockUser public function Setup user.
DataLayerUnitTests::testDataLayerAddDoesNotOverwriteByDefault public function Test DataLayer Add Does Not Overwrite By Default.
DataLayerUnitTests::testDataLayerAddWillAddData public function Test DataLayer Add Will Add Data.
DataLayerUnitTests::testDataLayerAddWillOverwriteWithFlag public function Test DataLayer Add Will Overwrite With Flag.
DataLayerUnitTests::testDataLayerDefaults public function Test DataLayer Defaults function.
DataLayerUnitTests::testDataLayerGetEntityDataReturnsEntityDataArray public function Test DataLayer Get Entity Terms Returns Entity Data Array.
DataLayerUnitTests::testDataLayerGetEntityTermsReturnsEmptyArray public function Test DataLayer Get Entity Terms Returns Empty Array.
DataLayerUnitTests::testDataLayerGetEntityTermsReturnsTermArray public function Test DataLayer Get Entity Terms Returns Term Array.
DataLayerUnitTests::testDataLayerMenuGetAnyObjectReturnsFalseWithIncorrectArgPosition public function Test DataLayer Menu Get Any Object.
DataLayerUnitTests::testDataLayerMenuGetAnyObjectReturnsFalseWithoutLoadFunctionMatch public function Test DataLayer Menu Get Any Object.
DataLayerUnitTests::testDataLayerMenuGetAnyObjectReturnsFalseWithoutLoadFunctions public function Test DataLayer Menu Get Any Object.
DataLayerUnitTests::testDataLayerMenuGetAnyObjectReturnsObject public function Test DataLayer Menu Get Any Object Returns Object.
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct