diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md index cd17c06..27deddf 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,26 @@ E.g '/node/add/.*', ]; ``` + +## CsvDownloadTrait + +A trait to interact with Views CSV exports making it easy to make assertions on CSV output. + +This trait requires the `league/csv` library which is included in the views_data_export module (via csv_serialization). + +### Usage + +Once your trait is added to your test base class, you can get CSV output into a variable and then make assertions on the contents. + +```php +$this->drupalGet('admin/content'); +// Pass TRUE if your CSV is batched, otherwise FALSE. +$csv = $this->assertViewsCsvExportRowCount(3, TRUE); +$csv->next(); +$row = $csv->current(); +$this->assertEquals([ + 'Title' => 'Foo bar', + 'Content type' => 'Basic', + 'Status' => 'Published', +], $row); +``` diff --git a/src/Traits/CsvDownloadTrait.php b/src/Traits/CsvDownloadTrait.php new file mode 100644 index 0000000..48dd266 --- /dev/null +++ b/src/Traits/CsvDownloadTrait.php @@ -0,0 +1,48 @@ +clickLink($csvLinkText); + $csvReader = $this->waitForCsv($isBatch); + $this->assertCount($count, $csvReader->getRecords()); + return $csvReader; + } + + /** + * Wait for a CSV to finish (batch or standard) and return a Reader. + */ + protected function waitForCsv(bool $isBatch = TRUE): Reader { + if ($isBatch) { + $this->checkForMetaRefresh(); + $this->assertSession()->pageTextContains('Export complete. Download the file here.'); + $this->clickLink('here'); + } + $csv = $this->getSession() + ->getDriver() + ->getContent(); + $csvReader = Reader::createFromString($csv); + if ($csvReader->getInputBOM() === Reader::BOM_UTF16_LE) { + CharsetConverter::addTo($csvReader, 'UTF-16', 'UTF-8'); + } + $csvReader->setHeaderOffset(0); + return $csvReader; + } + +}