Chris@0: drupalGet('common-test/type-link-active-class'); Chris@0: $this->assertIdentical(0, strpos($this->getRawContent(), "\nassertIdentical('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type')); Chris@0: $this->assertTitle('Test active link class | Drupal'); Chris@0: $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT); Chris@0: Chris@0: $this->drupalGet('common-test/type-link-active-class', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]); Chris@0: $this->assertIdentical('application/json', $this->drupalGetHeader('Content-Type')); Chris@0: $json = Json::decode($this->getRawContent()); Chris@0: $this->assertEqual(['content', 'title'], array_keys($json)); Chris@0: $this->assertIdentical('Test active link class', $json['title']); Chris@0: $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests rendering form elements without passing through Chris@0: * \Drupal::formBuilder()->doBuildForm(). Chris@0: */ Chris@0: public function testDrupalRenderFormElements() { Chris@0: // Define a series of form elements. Chris@0: $element = [ Chris@0: '#type' => 'button', Chris@0: '#value' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'submit']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#value' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'text']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'password', Chris@0: '#title' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'password']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'textarea', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#value' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//textarea'); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'radio', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#value' => FALSE, Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'radio']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'checkbox']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'select', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#options' => [ Chris@0: 0 => $this->randomMachineName(), Chris@0: 1 => $this->randomMachineName(), Chris@0: ], Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//select'); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'file', Chris@0: '#title' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'file']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'item', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#markup' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', [ Chris@0: ':class' => 'js-form-type-item', Chris@0: ':markup' => $element['#markup'], Chris@0: ':label' => $element['#title'], Chris@0: ]); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'hidden', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#value' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'hidden']); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'link', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#url' => Url::fromRoute('common_test.destination'), Chris@0: '#options' => [ Chris@0: 'absolute' => TRUE, Chris@0: ], Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', [ Chris@0: ':href' => URL::fromRoute('common_test.destination')->setAbsolute()->toString(), Chris@0: ':title' => $element['#title'], Chris@0: ]); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'details', Chris@0: '#open' => TRUE, Chris@0: '#title' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//details/summary[contains(., :title)]', [ Chris@0: ':title' => $element['#title'], Chris@0: ]); Chris@0: Chris@0: $element = [ Chris@0: '#type' => 'details', Chris@0: '#open' => TRUE, Chris@0: '#title' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//details'); Chris@0: Chris@0: $element['item'] = [ Chris@0: '#type' => 'item', Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#markup' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->assertRenderedElement($element, '//details/div/div[contains(@class, :class) and contains(., :markup)]', [ Chris@0: ':class' => 'js-form-type-item', Chris@0: ':markup' => $element['item']['#markup'], Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that elements are rendered properly. Chris@0: */ Chris@0: protected function assertRenderedElement(array $element, $xpath, array $xpath_args = []) { Chris@0: $original_element = $element; Chris@0: $this->setRawContent(drupal_render_root($element)); Chris@0: $this->verbose('
' . $this->getRawContent()); Chris@0: Chris@0: // @see \Drupal\simpletest\WebTestBase::xpath() Chris@0: $xpath = $this->buildXPathQuery($xpath, $xpath_args); Chris@0: $element += ['#value' => NULL]; Chris@0: $this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', [ Chris@0: '@type' => var_export($element['#type'], TRUE), Chris@0: ])); Chris@0: } Chris@0: Chris@0: }