From a872fa3d28ddd2b6aa5fe55fe17c45fa721745a7 Mon Sep 17 00:00:00 2001 From: Adam Bramley Date: Tue, 23 Jul 2024 14:48:04 +1000 Subject: [PATCH 1/3] Add CSV download trait --- .gitignore | 1 + README.md | 23 +++++++++++++++ src/Traits/CsvDownloadTrait.php | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 .gitignore create mode 100644 src/Traits/CsvDownloadTrait.php 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..4f36d8a 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', +]); +``` diff --git a/src/Traits/CsvDownloadTrait.php b/src/Traits/CsvDownloadTrait.php new file mode 100644 index 0000000..711aa35 --- /dev/null +++ b/src/Traits/CsvDownloadTrait.php @@ -0,0 +1,51 @@ +clickLink('Download CSV'); + $csvReader = $this->waitForCsv($isBatch); + $this->assertCount($count, $csvReader->getRecords()); + return $csvReader; + } + + /** + * Asserts that a CSV export has a certain number of rows. + * + * Clicks the orange 'CSV' button displayed in views, waits for batch to run, + * then asserts contents of download. + */ + 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; + } + +} From fa3a9d8ebdd65a9c418e13718430a4a867330347 Mon Sep 17 00:00:00 2001 From: Adam Bramley Date: Tue, 23 Jul 2024 15:05:49 +1000 Subject: [PATCH 2/3] Fixes --- src/Traits/CsvDownloadTrait.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Traits/CsvDownloadTrait.php b/src/Traits/CsvDownloadTrait.php index 711aa35..48dd266 100644 --- a/src/Traits/CsvDownloadTrait.php +++ b/src/Traits/CsvDownloadTrait.php @@ -18,18 +18,15 @@ trait CsvDownloadTrait { * Clicks the orange 'CSV' button displayed in views, waits for batch to run, * then asserts contents of download. */ - protected function assertViewsCsvExportRowCount(int $count, bool $isBatch = TRUE): Reader { - $this->clickLink('Download CSV'); + protected function assertViewsCsvExportRowCount(int $count, bool $isBatch = TRUE, string $csvLinkText = 'Download CSV'): Reader { + $this->clickLink($csvLinkText); $csvReader = $this->waitForCsv($isBatch); $this->assertCount($count, $csvReader->getRecords()); return $csvReader; } /** - * Asserts that a CSV export has a certain number of rows. - * - * Clicks the orange 'CSV' button displayed in views, waits for batch to run, - * then asserts contents of download. + * Wait for a CSV to finish (batch or standard) and return a Reader. */ protected function waitForCsv(bool $isBatch = TRUE): Reader { if ($isBatch) { From e75346a634859889c95b20721e3093f9e317c13a Mon Sep 17 00:00:00 2001 From: Adam Bramley Date: Tue, 23 Jul 2024 16:05:19 +1000 Subject: [PATCH 3/3] Fix assert --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f36d8a..27deddf 100644 --- a/README.md +++ b/README.md @@ -118,5 +118,5 @@ $this->assertEquals([ 'Title' => 'Foo bar', 'Content type' => 'Basic', 'Status' => 'Published', -]); +], $row); ```