<?php
// $Id: purl.test,v 1.1.2.2 2009-12-18 17:25:16 jmiccolis Exp $

/**
 * Unit tests for PURL.
 */
class PURLUnitTestCase extends DrupalWebTestCase {
  /**
   * Test info.
   */
  public function getInfo() {
    return array(
      'name' => t('Unit tests'),
      'description' => t('Unit tests for PURL.') ,
      'group' => t('PURL'),
    );
  }

  /**
   * Set up test.
   */
  public function setUp() {
    parent::setUp(
      'ctools',
      'purl',
      'purl_test'
    );
    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
    $this->drupalLogin($admin_user);
  }

  /**
   * Run test.
   */
  public function test() {
    // API aspects to test
    // - PURL activation
    // - URL rewriting
    // - PURL disabling
    // - PURL provider removal
    // - PURL provider addition

    variable_set('purl_types', array(
      'path' => 'path',
      'pair' => 'pair',
      'extension' => 'extension',
      'querystring' => 'querystring'
    ));

    // Test with both providers set to path.
    variable_set('purl_method_purl_test', 'path');
    variable_set('purl_method_purl_alt', 'path');
    $testname = t('Path + Path');

    $output = $this->drupalGet('admin/build/testing/purl');
    $this->assertNoText('PURL test ID', t('@test: Modifier not present.', array('@test' => $testname)));
    $this->assertText('Rewrite: /node', t('@test: Link not rewritten.', array('@test' => $testname)));
    $this->assertText('Disable providers: /node', t('@test: Provider disabled.', array('@test' => $testname)));
    $this->assertText('Remove provider: /node', t('@test: Provider removed.', array('@test' => $testname)));
    $this->assertText('Add provider: /sweden/node', t('@test: Provider added.', array('@test' => $testname)));

    $output = $this->drupalGet('foo/admin/build/testing/purl');
    $this->assertText('PURL test ID: foo', t('@test: Modifier parsed.', array('@test' => $testname)));
    $this->assertText('Rewrite: /foo/node', t('@test: Link rewritten.', array('@test' => $testname)));
    $this->assertText('Disable providers: /node', t('@test: Provider disabled.', array('@test' => $testname)));
    $this->assertText('Remove provider: /node', t('@test: Provider removed.', array('@test' => $testname)));
    $this->assertText('Add provider: /sweden/foo/node', t('@test: Provider added.', array('@test' => $testname)));

    // Test path + pair.
    variable_set('purl_method_purl_alt', 'pair');
    variable_set('purl_method_purl_alt_key', 'altkey');
    $testname = t('Path + Pair');

    $output = $this->drupalGet('admin/build/testing/purl');
    $this->assertNoText('PURL test ID', t('@test: Modifier not present.', array('@test' => $testname)));
    $this->assertText('Rewrite: /node', t('@test: Link not rewritten.', array('@test' => $testname)));
    $this->assertText('Disable providers: /node', t('@test: Provider disabled.', array('@test' => $testname)));
    $this->assertText('Remove provider: /node', t('@test: Provider removed.', array('@test' => $testname)));
    $this->assertText('Add provider: /altkey/sweden/node', t('@test: Provider added.', array('@test' => $testname)));

    $output = $this->drupalGet('foo/admin/build/testing/purl');
    $this->assertText('PURL test ID: foo', t('@test: Modifier parsed.', array('@test' => $testname)));
    $this->assertText('Rewrite: /foo/node', t('@test: Link rewritten.', array('@test' => $testname)));
    $this->assertText('Disable providers: /node', t('@test: Provider disabled.', array('@test' => $testname)));
    $this->assertText('Remove provider: /node', t('@test: Provider removed.', array('@test' => $testname)));
    $this->assertText('Add provider: /altkey/sweden/foo/node', t('@test: Provider added.', array('@test' => $testname)));

    // Test path + querystring.
    variable_set('purl_method_purl_alt', 'querystring');
    variable_set('purl_method_purl_alt_key', 'altkey');
    $testname = t('Path + Querystring');

    $output = $this->drupalGet('admin/build/testing/purl');
    $this->assertNoText('PURL test ID', t('@test: Modifier not present.', array('@test' => $testname)));
    $this->assertText('Rewrite: /node', t('@test: Link not rewritten.', array('@test' => $testname)));
    $this->assertText('Disable providers: /node', t('@test: Provider disabled.', array('@test' => $testname)));
    $this->assertText('Remove provider: /node', t('@test: Provider removed.', array('@test' => $testname)));
    $this->assertText('Add provider: /node?altkey=sweden', t('@test: Provider added.', array('@test' => $testname)));

    $output = $this->drupalGet('foo/admin/build/testing/purl');
    $this->assertText('PURL test ID: foo', t('@test: Modifier parsed.', array('@test' => $testname)));
    $this->assertText('Rewrite: /foo/node', t('@test: Link rewritten.', array('@test' => $testname)));
    $this->assertText('Disable providers: /node', t('@test: Provider disabled.', array('@test' => $testname)));
    $this->assertText('Remove provider: /node', t('@test: Provider removed.', array('@test' => $testname)));
    $this->assertText('Add provider: /foo/node?altkey=sweden', t('@test: Provider added.', array('@test' => $testname)));
  }
}