BlockFormInBlockTest.php 2.18 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\block\Functional;
4

5
use Drupal\Component\Utility\Crypt;
6
use Drupal\Tests\BrowserTestBase;
7 8 9 10 11 12

/**
 * Tests form in block caching.
 *
 * @group block
 */
13
class BlockFormInBlockTest extends BrowserTestBase {
14 15 16 17 18 19

  /**
   * Modules to install.
   *
   * @var array
   */
20
  protected static $modules = ['block', 'block_test', 'test_page_test'];
21

22 23 24 25 26
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

27 28 29
  /**
   * {@inheritdoc}
   */
30
  protected function setUp(): void {
31 32 33
    parent::setUp();

    // Enable our test block.
34
    $this->drupalPlaceBlock('test_form_in_block');
35 36 37 38 39
  }

  /**
   * Test to see if form in block's redirect isn't cached.
   */
40
  public function testCachePerPage() {
41 42 43 44
    $form_values = ['email' => 'test@example.com'];

    // Go to "test-page" and test if the block is enabled.
    $this->drupalGet('test-page');
45
    $this->assertSession()->statusCodeEquals(200);
46 47 48 49 50
    $this->assertText('Your .com email address.', 'form found');

    // Make sure that we're currently still on /test-page after submitting the
    // form.
    $this->drupalPostForm(NULL, $form_values, t('Submit'));
51
    $this->assertSession()->addressEquals('test-page');
52 53 54 55
    $this->assertText(t('Your email address is @email', ['@email' => 'test@example.com']));

    // Go to a different page and see if the block is enabled there as well.
    $this->drupalGet('test-render-title');
56
    $this->assertSession()->statusCodeEquals(200);
57 58 59 60 61 62
    $this->assertText('Your .com email address.', 'form found');

    // Make sure that submitting the form didn't redirect us to the first page
    // we submitted the form from after submitting the form from
    // /test-render-title.
    $this->drupalPostForm(NULL, $form_values, t('Submit'));
63
    $this->assertSession()->addressEquals('test-render-title');
64 65 66 67 68 69 70 71 72
    $this->assertText(t('Your email address is @email', ['@email' => 'test@example.com']));
  }

  /**
   * Test the actual placeholders
   */
  public function testPlaceholders() {
    $this->drupalGet('test-multiple-forms');

73
    $placeholder = 'form_action_' . Crypt::hashBase64('Drupal\Core\Form\FormBuilder::prepareForm');
74 75 76 77
    $this->assertText('Form action: ' . $placeholder, 'placeholder found.');
  }

}