Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSV download trait #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
acbramley marked this conversation as resolved.
Show resolved Hide resolved
```
51 changes: 51 additions & 0 deletions src/Traits/CsvDownloadTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace PNX\DrupalTestUtils\Traits;

use League\Csv\CharsetConverter;
use League\Csv\Reader;

/**
* CSV download trait.
*/
trait CsvDownloadTrait {

/**
* 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 assertViewsCsvExportRowCount(int $count, bool $isBatch = TRUE): Reader {
$this->clickLink('Download CSV');
acbramley marked this conversation as resolved.
Show resolved Hide resolved
$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;
}

}