You are here

class AuthcacheP13nTestFragmentBuilder in Authenticated User Page Caching (Authcache) 7.2

Tests fragment builder.

Hierarchy

Expanded class hierarchy of AuthcacheP13nTestFragmentBuilder

File

modules/authcache_p13n/tests/authcache_p13n.request-handler.test, line 263
Define unit tests for request handler.

View source
class AuthcacheP13nTestFragmentBuilder extends DrupalUnitTestCase {
  protected $stubObserver;
  protected $fragmentValidator;
  protected $fragementLoader;
  protected $fragmentAccess;
  protected $fragmentRenderer;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Fragment Builder',
      'description' => 'Unit tests for fragment builder.',
      'group' => 'Authcache Personalization',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    require_once __DIR__ . '/../includes/AuthcacheP13nFragmentBuilder.inc';
    $this->stubObserver = new AuthcacheP13nTestStubObserver();
    $this->fragmentValidator = new AuthcacheP13nTestFragmentValidatorStub($this->stubObserver);
    $this->fragmentLoader = new AuthcacheP13nTestFragmentLoaderStub($this->stubObserver);
    $this->fragmentAccess = new AuthcacheP13nTestFragmentAccessStub($this->stubObserver);
    $this->fragmentRenderer = new AuthcacheP13nTestFragmentStub($this->stubObserver);
    parent::setUp();
  }

  /**
   * Tests fragment request.
   */
  public function testFragmentBuilder() {
    $input = array(
      'a' => array(
        'some_key' => 'some_subject',
      ),
    );
    $context = array(
      'bla',
    );
    $builder = new AuthcacheP13nFragmentBuilder($this->fragmentRenderer, NULL, NULL, NULL);

    // Setup expectations.
    $render = $this->stubObserver
      ->method($this->fragmentRenderer, 'render', 'rendered')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'some_key',
      'some_subject',
      $context,
    )));

    // Run builder.
    $output = $builder
      ->build($input, $context);
    $this
      ->assertEqual('rendered', $output);

    // Verify stub.
    $this
      ->assert($render
      ->verify($message), $message);
  }

  /**
   * Test request with validator.
   */
  public function testFragmentBuilderValidation() {
    $builder = new AuthcacheP13nFragmentBuilder($this->fragmentRenderer, $this->fragmentValidator, NULL, NULL);

    // Setup expectations.
    $validator = $this->stubObserver
      ->method($this->fragmentValidator, 'validate', array(
      'some_key' => 'valid value',
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      array(
        'some_key' => 'some_key',
      ),
    )));

    // Run validator.
    $result = $builder
      ->validate(array(
      'a' => 'some_key',
    ));

    // Verify stub.
    $this
      ->assertEqual(array(
      'a' => array(
        'some_key' => 'valid value',
      ),
    ), $result);
    $this
      ->assert($validator
      ->verify($message), $message);

    // Setup expectations.
    $validator = $this->stubObserver
      ->method($this->fragmentValidator, 'validate', new AuthcacheP13nRequestInvalidInput())
      ->expect(AuthcacheP13nTestStubVerifyer::once());

    // Run validator.
    $input = array(
      'a' => 'invalid',
    );
    try {
      $builder
        ->validate($input);
      $this
        ->fail('AuthcacheP13nFragmentBuilder should throw an AuthcacheP13nRequestInvalidInput when validation fails');
    } catch (AuthcacheP13nRequestInvalidInput $e) {
      unset($e);
      $this
        ->pass('AuthcacheP13nFragmentBuilder should throw an AuthcacheP13nRequestInvalidInput when validation fails');
    }

    // Verify stub.
    $this
      ->assert($validator
      ->verify($message), $message);
  }

  /**
   * Test request with loader.
   */
  public function testFragmentBuilderLoader() {
    $context = array(
      'some' => 'context',
    );
    $builder = new AuthcacheP13nFragmentBuilder($this->fragmentRenderer, NULL, $this->fragmentLoader, NULL);

    // Setup expectations.
    $load = $this->stubObserver
      ->method($this->fragmentLoader, 'load', array(
      'some_key' => 'loaded subject',
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      array(
        'some_key' => 'some_subject',
      ),
      $context,
    )));
    $render = $this->stubObserver
      ->method($this->fragmentRenderer, 'render', 'rendered')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'some_key',
      'loaded subject',
      $context,
    )));

    // Run builder.
    $input = array(
      'a' => array(
        'some_key' => 'some_subject',
      ),
    );
    $output = $builder
      ->build($input, $context);
    $this
      ->assertEqual('rendered', $output);

    // Verify stub.
    $this
      ->assert($load
      ->verify($message), $message);
    $this
      ->assert($render
      ->verify($message), $message);

    // Setup expectations.
    $load = $this->stubObserver
      ->method($this->fragmentLoader, 'load', new AuthcacheP13nRequestNotFound())
      ->expect(AuthcacheP13nTestStubVerifyer::once());
    $render = $this->stubObserver
      ->method($this->fragmentRenderer, 'render')
      ->expect(AuthcacheP13nTestStubVerifyer::never());

    // Run builder.
    $input = array(
      'a' => array(
        'some_key' => 'missing',
      ),
    );
    try {
      $builder
        ->build($input, array());
      $this
        ->fail('AuthcacheP13nFragmentBuilder should throw an AuthcacheP13nRequestNotFound when loading fails');
    } catch (AuthcacheP13nRequestNotFound $e) {
      unset($e);
      $this
        ->pass('AuthcacheP13nFragmentBuilder should throw an AuthcacheP13nRequestNotFound when loading fails');
    }

    // Verify stub.
    $this
      ->assert($load
      ->verify($message), $message);
    $this
      ->assert($render
      ->verify($message), $message);
  }

  /**
   * Test request with access checker.
   */
  public function testFragmentBuilderAccessChecker() {
    global $user;
    $orig_user = $user;
    $fake_user = (object) array(
      'uid' => 42,
      'mail' => 'fake@example.com',
      'name' => 'fake',
    );
    $context = array(
      'some' => 'context',
    );

    // Setup expectations.
    $check = $this->stubObserver
      ->method($this->fragmentAccess, 'check', TRUE)
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $fake_user,
      'some_key',
      'some_subject',
      $context,
    )));
    $render = $this->stubObserver
      ->method($this->fragmentRenderer, 'render', 'rendered')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'some_key',
      'some_subject',
      $context,
    )));

    // Run builder.
    $user = $fake_user;
    $input = array(
      'a' => array(
        'some_key' => 'some_subject',
      ),
    );
    $builder = new AuthcacheP13nFragmentBuilder($this->fragmentRenderer, NULL, NULL, $this->fragmentAccess);
    $result = $builder
      ->build($input, $context);
    $user = $orig_user;

    // Verify stub.
    $this
      ->assertEqual('rendered', $result);
    $this
      ->assert($check
      ->verify($message), $message);
    $this
      ->assert($render
      ->verify($message), $message);

    // Setup expectations.
    $check = $this->stubObserver
      ->method($this->fragmentAccess, 'check', FALSE)
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $fake_user,
      'some_key',
      'some_subject',
      $context,
    )));
    $render = $this->stubObserver
      ->method($this->fragmentRenderer, 'render')
      ->expect(AuthcacheP13nTestStubVerifyer::never());

    // Run builder.
    $user = $fake_user;
    $input = array(
      'a' => array(
        'some_key' => 'some_subject',
      ),
    );
    $builder = new AuthcacheP13nFragmentBuilder($this->fragmentRenderer, NULL, NULL, $this->fragmentAccess);
    try {
      $builder
        ->build($input, $context);
      $this
        ->fail('AuthcacheP13nFragmentBuilder should throw an AuthcacheP13nRequestAccessDenied when access check fails');
    } catch (AuthcacheP13nRequestAccessDenied $e) {
      unset($e);
      $this
        ->pass('AuthcacheP13nFragmentBuilder should throw an AuthcacheP13nRequestAccessDenied when access check fails');
    }
    $user = $orig_user;

    // Verify stub.
    $this
      ->assert($check
      ->verify($message), $message);
    $this
      ->assert($render
      ->verify($message), $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthcacheP13nTestFragmentBuilder::$fragementLoader protected property
AuthcacheP13nTestFragmentBuilder::$fragmentAccess protected property
AuthcacheP13nTestFragmentBuilder::$fragmentRenderer protected property
AuthcacheP13nTestFragmentBuilder::$fragmentValidator protected property
AuthcacheP13nTestFragmentBuilder::$stubObserver protected property
AuthcacheP13nTestFragmentBuilder::getInfo public static function
AuthcacheP13nTestFragmentBuilder::setUp public function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
AuthcacheP13nTestFragmentBuilder::testFragmentBuilder public function Tests fragment request.
AuthcacheP13nTestFragmentBuilder::testFragmentBuilderAccessChecker public function Test request with access checker.
AuthcacheP13nTestFragmentBuilder::testFragmentBuilderLoader public function Test request with loader.
AuthcacheP13nTestFragmentBuilder::testFragmentBuilderValidation public function Test request with validator.
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