You are here

public function AuthcacheP13nTestConfigWidget::testFormElementClients in Authenticated User Page Caching (Authcache) 7.2

Cover form element authcache_p13n_config.

File

modules/authcache_p13n/tests/authcache_p13n.config.test, line 180
Defines test for authcache personalization configuration widget.

Class

AuthcacheP13nTestConfigWidget
Tests for authcache_p13n_config element.

Code

public function testFormElementClients() {
  $form['authcache-settings'] = array(
    '#type' => 'authcache_p13n_config',
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Test'),
  );
  $clients = array(
    'a' => array(
      'title' => 'A',
      'enabled' => TRUE,
    ),
    'b' => array(
      'title' => 'B',
      'enabled' => TRUE,
    ),
    'c' => array(
      'title' => 'C',
      'enabled' => FALSE,
    ),
    'd' => array(
      'title' => 'D',
      'enabled' => FALSE,
    ),
  );
  $form_stub = $this->stubmod
    ->hook('form', $form);
  $this->stubmod
    ->hook('authcache_p13n_client', $clients);
  $form_submit_stub = $this->stubmod
    ->hook('form_submit');

  // Get form and check presence of config widget.
  $this
    ->drupalGet('authcache-p13n-test-form');
  $this
    ->assertStub($form_stub, HookStub::once());

  // By default all clients should be selected.
  $this
    ->assertFieldChecked('edit-authcache-settings-clients-a-status');
  $this
    ->assertFieldChecked('edit-authcache-settings-clients-b-status');
  $this
    ->assertFieldChecked('edit-authcache-settings-clients-c-status');
  $this
    ->assertFieldChecked('edit-authcache-settings-clients-d-status');

  // By default all weights should be 0.
  $this
    ->assertFieldById('edit-authcache-settings-clients-a-weight', 0);
  $this
    ->assertFieldById('edit-authcache-settings-clients-b-weight', 0);
  $this
    ->assertFieldById('edit-authcache-settings-clients-c-weight', 0);
  $this
    ->assertFieldById('edit-authcache-settings-clients-d-weight', 0);
  $edit = array(
    'authcache-settings[status]' => TRUE,
    'authcache-settings[lifespan][select]' => 3600,
    'authcache-settings[lifespan][custom]' => '',
    'authcache-settings[peruser]' => TRUE,
    'authcache-settings[perpage]' => FALSE,
    'authcache-settings[clients][a][status]' => TRUE,
    'authcache-settings[clients][b][status]' => FALSE,
    'authcache-settings[clients][c][status]' => TRUE,
    'authcache-settings[clients][d][status]' => FALSE,
    'authcache-settings[clients][a][weight]' => -10,
    'authcache-settings[clients][b][weight]' => -20,
    'authcache-settings[clients][c][weight]' => -30,
    'authcache-settings[clients][d][weight]' => -40,
    'authcache-settings[fallback]' => 'cancel',
  );
  $this
    ->drupalPost(NULL, $edit, t('Test'));
  $this
    ->assertStub($form_submit_stub, HookStub::once());
  $invocations = $form_submit_stub
    ->invocations();
  $values = $invocations[0][1]['values'];
  $expect = array(
    'status' => '1',
    'lifespan' => '3600',
    'peruser' => '1',
    'perpage' => '0',
    'clients' => array(
      'a' => array(
        'status' => '1',
        'weight' => '-10',
      ),
      'b' => array(
        'status' => '0',
        'weight' => '-20',
      ),
      'c' => array(
        'status' => '1',
        'weight' => '-30',
      ),
      'd' => array(
        'status' => '0',
        'weight' => '-40',
      ),
    ),
    'fallback' => 'cancel',
  );
  $this
    ->assertEqual($expect, $values['authcache-settings']);
}