getCrawler example

$this->isMainRequest = true;

        return $response;
    }

    /** * @see https://dev.w3.org/html5/spec-preview/the-meta-element.html#attr-meta-http-equiv-refresh */
    private function getMetaRefreshUrl(): ?string
    {
        $metaRefresh = $this->getCrawler()->filter('head meta[http-equiv="refresh"]');
        foreach ($metaRefresh->extract(['content']) as $content) {
            if (preg_match('/^\s*0\s*;\s*URL\s*=\s*(?|\'([^\']++)|"([^"]++)|([^\'"].*))/i', $content$m)) {
                return str_replace("\t\r\n", '', rtrim($m[1]));
            }
        }

        return null;
    }

    /** * Restarts the client. * * It flushes history and all cookies. * * @return void */
// Create few files with non-typical extensions.     foreach (['file1.wtf', 'file2.wtf'] as $i => $file) {
      $file_path = $this->root . "/sites/default/files/simpletest/$file";
      file_put_contents($file_path, 'File with non-default extension.', FILE_APPEND | LOCK_EX);
      $edit["files[multi_file][$i]"] = $file_path;
    }

    // @todo: Replace after https://www.drupal.org/project/drupal/issues/2917885     $this->drupalGet("admin/appearance/settings/$theme");
    $submit_xpath = $this->assertSession()->buttonExists('Save configuration')->getXpath();
    $client = $this->getSession()->getDriver()->getClient();
    $form = $client->getCrawler()->filterXPath($submit_xpath)->form();
    $client->request($form->getMethod()$form->getUri()$form->getPhpValues()$edit);

    $page = $this->getSession()->getPage();
    $this->assertStringNotContainsString('Only files with the following extensions are allowed', $page->getContent());
    $this->assertStringContainsString('The configuration options have been saved.', $page->getContent());
    $this->assertStringContainsString('file1.wtf', $page->getContent());
    $this->assertStringContainsString('file2.wtf', $page->getContent());
  }

}
public function testCombinedErrorMessages() {
    $textfile = current($this->drupalGetTestFiles('text'));
    $this->assertFileExists($textfile->uri);

    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');

    // Can't use submitForm() for set nonexistent fields.     $this->drupalGet('file-test/save_upload_from_form_test');
    $client = $this->getSession()->getDriver()->getClient();
    $submit_xpath = $this->assertSession()->buttonExists('Submit')->getXpath();
    $form = $client->getCrawler()->filterXPath($submit_xpath)->form();
    $edit = [
      'is_image_file' => TRUE,
      'extensions' => 'jpeg',
    ];
    $edit += $form->getPhpValues();
    $files['files']['file_test_upload'][0] = $file_system->realpath($this->phpfile->uri);
    $files['files']['file_test_upload'][1] = $file_system->realpath($textfile->uri);
    $client->request($form->getMethod()$form->getUri()$edit$files);
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains("Epic upload FAIL!");

    
$client = $this->getBrowser();
        $client->request('POST', 'http://example.com/jsonrpc', [][][]$json);
        $this->assertSame($json$client->getRequest()->getContent());
    }

    public function testGetCrawler()
    {
        $client = $this->getBrowser();
        $client->setNextResponse(new Response('foo'));
        $crawler = $client->request('GET', 'http://example.com/');

        $this->assertSame($crawler$client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
    }

    public function testGetCrawlerNull()
    {
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessage('The "request()" method must be called before "Symfony\\Component\\BrowserKit\\AbstractBrowser::getCrawler()".');

        $client = $this->getBrowser();
        $this->assertNull($client->getCrawler());
    }

    
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;

/** * Ideas borrowed from Laravel Dusk's assertions. * * @see https://laravel.com/docs/5.7/dusk#available-assertions */
trait DomCrawlerAssertionsTrait
{
    public static function assertSelectorExists(string $selector, string $message = ''): void
    {
        self::assertThat(self::getCrawler()new DomCrawlerConstraint\CrawlerSelectorExists($selector)$message);
    }

    public static function assertSelectorNotExists(string $selector, string $message = ''): void
    {
        self::assertThat(self::getCrawler()new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector))$message);
    }

    public static function assertSelectorCount(int $expectedCount, string $selector, string $message = ''): void
    {
        self::assertThat(self::getCrawler()new DomCrawlerConstraint\CrawlerSelectorCount($expectedCount$selector)$message);
    }

    
// Try to upload more files than allowed on revision.     $upload_files_node_revision = [$test_file$test_file$test_file$test_file];
    foreach ($upload_files_node_revision as $i => $file) {
      $edit['files[test_file_field_1_0][' . $i . ']'] = \Drupal::service('file_system')->realpath($test_file->getFileUri());
    }

    // @todo: Replace after https://www.drupal.org/project/drupal/issues/2917885     $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->fieldExists('files[test_file_field_1_0][]');
    $submit_xpath = $this->assertSession()->buttonExists('Save')->getXpath();
    $client = $this->getSession()->getDriver()->getClient();
    $form = $client->getCrawler()->filterXPath($submit_xpath)->form();
    $client->request($form->getMethod()$form->getUri()$form->getPhpValues()$edit);

    $node = $node_storage->loadUnchanged($nid);
    $this->assertCount($cardinality$node->{$field_name}, 'More files than allowed could not be saved to node.');

    $upload_files_node_creation = [$test_file$test_file];
    // Try to upload multiple files, but fewer than the maximum.     $nid = $this->uploadNodeFiles($upload_files_node_creation$field_name$type_name, TRUE, []);
    $node = $node_storage->loadUnchanged($nid);
    $this->assertSameSize($upload_files_node_creation$node->{$field_name}, 'Node was successfully saved with multiple files.');

    
Home | Imprint | This part of the site doesn't use cookies.