<?php

class ContextUnitTest extends DrupalWebTestCase {
  protected $profile = 'testing';

  public static function getInfo() {
    return array(
      'name' => 'API unit tests',
      'description' => 'Sets all possible context types and checks for integrity.',
      'group' => 'Context',
    );
  }

  public function setUp() {
    parent::setUp('context');
  }

  public function test() {
    // define possible data types
    $set_types = array(
      'bool'    => TRUE,
      'int'     => 1,
      'string'  => 'lorem',
      'array'   => array('lorem'),
      'object'  => new stdClass(),
    );
    $id_types = array('int', 'string');

    // NAMESPACE
    foreach ($set_types as $type => $val) {
      $set = context_set($val);
      // Test return value of context_set()
      if (in_array($type, $id_types)) {
        // test set integrity
        $this->assertIdentical(true, $set, 'Space set successful.');
        // test get integrity
        $this->assertIdentical(array(), context_get($val), 'Namespace get successful.');
        $this->assertIdentical(true, context_exists($val), 'Namespace exists successful.');
      }
      else {
        $this->assertIdentical(false, $set, 'Prohibited namespace not established.');
      }
      context_clear();
    }

    // NAMESPACE+ATTRIBUTE
    foreach ($set_types as $type => $val) {
      foreach ($set_types as $type2 => $val2) {
        // test set integrity
        $set = context_set($val, $val2);
        if (in_array($type, $id_types)) {
          // test set integrity
          if ($type2 != 'bool') {
            $this->assertIdentical(true, $set, 'Namespace and attribute set successful.');
          }
          else {
            $this->assertIdentical(false, $set);
          }
          // test get + exists integrity
          if (in_array($type2, $id_types)) {
            $this->assertIdentical(true, (context_get($val, $val2) == $val2), 'Namespace and attribute get successful.');
            $this->assertIdentical(true, context_exists($val, $val2), 'Namespace and attribute exists.');
          }
          elseif (in_array($type2, array('array', 'object'))) {
            $this->assertIdentical(true, (context_get($val) == $val2), 'Namespace and attribute get successful.');
            $this->assertIdentical(true, context_exists($val), 'Namespace and attribute exists.');
          }
        }
      }
      context_clear();
    }

    // NAMESPACE+ATTRIBUTE+VALUE, o lord
    foreach ($set_types as $type => $val) {
      foreach ($set_types as $type2 => $val2) {
        foreach ($set_types as $type3 => $val3) {
          $set = context_set($val, $val2, $val3);
          if (in_array($type, $id_types)) {
            if (in_array($type2, $id_types)) {
              $this->assertIdentical(true, (context_get($val, $val2, $val3) == $val3), 'Namespace, attribute and value get successful.');
              $this->assertIdentical(true, context_exists($val, $val2, $val3), 'Namespace, attribute and value exists.');
            }
          }
          context_clear();
        }
      }
    }
  }
}
