You are here

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

Cover form element authcache_p13n_config.

File

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

Class

AuthcacheP13nTestConfigWidget
Tests for authcache_p13n_config element.

Code

public function testFormElement() {
  $form['authcache-settings'] = array(
    '#type' => 'authcache_p13n_config',
  );
  $form['other-authcache-settings'] = array(
    '#type' => 'authcache_p13n_config',
    '#default_value' => array(
      'status' => TRUE,
      'lifespan' => 42,
      'peruser' => 0,
      'perpage' => 1,
      'fallback' => 'hide',
    ),
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Test'),
  );
  $form_stub = $this->stubmod
    ->hook('form', $form);
  $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());

  // First config field.
  $elements = $this
    ->xpath('//div[@id="edit-authcache-settings"]');
  $this
    ->assertEqual(1, count($elements));
  $this
    ->assertNoFieldChecked('edit-authcache-settings-status');
  $this
    ->assertOptionSelected('edit-authcache-settings-lifespan-select', 3600);
  $this
    ->assertFieldChecked('edit-authcache-settings-peruser');
  $this
    ->assertNoFieldChecked('edit-authcache-settings-perpage');
  $this
    ->assertFieldChecked('edit-authcache-settings-fallback-cancel');
  $this
    ->assertNoFieldChecked('edit-authcache-settings-fallback-hide');

  // Second config widget.
  $elements = $this
    ->xpath('//div[@id="edit-other-authcache-settings"]');
  $this
    ->assertEqual(1, count($elements));
  $this
    ->assertFieldChecked('edit-other-authcache-settings-status');
  $this
    ->assertOptionSelected('edit-other-authcache-settings-lifespan-select', 'custom');
  $this
    ->assertFieldById('edit-other-authcache-settings-lifespan-custom', 42);
  $this
    ->assertNoFieldChecked('edit-other-authcache-settings-peruser');
  $this
    ->assertFieldChecked('edit-other-authcache-settings-perpage');
  $this
    ->assertNoFieldChecked('edit-other-authcache-settings-fallback-cancel');
  $this
    ->assertFieldChecked('edit-other-authcache-settings-fallback-hide');

  // Submit form.
  $edit = array(
    'authcache-settings[status]' => TRUE,
    'authcache-settings[lifespan][select]' => 518400,
    'authcache-settings[lifespan][custom]' => '',
    'authcache-settings[peruser]' => TRUE,
    'authcache-settings[perpage]' => TRUE,
    'authcache-settings[fallback]' => 'cancel',
    'other-authcache-settings[status]' => TRUE,
    'other-authcache-settings[lifespan][select]' => 'custom',
    'other-authcache-settings[lifespan][custom]' => 42,
    'other-authcache-settings[fallback]' => 'hide',
  );
  $this
    ->drupalPost(NULL, $edit, t('Test'));
  $this
    ->assertStub($form_submit_stub, HookStub::once());
  $invocations = $form_submit_stub
    ->invocations();
  $values = $invocations[0][1]['values'];
  $expect_settings_1 = array(
    'status' => '1',
    'lifespan' => '518400',
    'peruser' => '1',
    'perpage' => '1',
    'fallback' => 'cancel',
  );
  $this
    ->assertEqual($expect_settings_1, $values['authcache-settings']);
  $expect_settings_2 = array(
    'status' => '1',
    'lifespan' => '42',
    'peruser' => 0,
    'perpage' => '1',
    'fallback' => 'hide',
  );
  $this
    ->assertEqual($expect_settings_2, $values['other-authcache-settings']);
}