Skip to content

Instantly share code, notes, and snippets.

@mecmartini
Created February 11, 2021 15:04
Show Gist options
  • Save mecmartini/1727abb4e387571630d5bc27ee60247b to your computer and use it in GitHub Desktop.
Save mecmartini/1727abb4e387571630d5bc27ee60247b to your computer and use it in GitHub Desktop.
Drupal - Test CSV
<?php
namespace Drupal\Tests\tablefield\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Simple test to ensure that a field can be created.
*
* @group tablefield
*/
class TableFieldExportCsvExportFileTest extends BrowserTestBase {
use TablefieldCreationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'tablefield'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->rootUser);
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
$this->createTableField('field_table', 'article', [], ['export' => 1]);
}
/**
* Create a node with a tablefield, and ensure it's displayed correctly.
*/
public function testTableField() {
$this->drupalGet('node/add/article');
// Create a node.
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool';
$edit['body[0][value]'] = 'Llamas are very cool';
$edit['field_table[0][caption]'] = 'Table caption';
$edit['field_table[0][tablefield][table][0][0]'] = 'Header 1';
$edit['field_table[0][tablefield][table][0][1]'] = 'Header 2';
$edit['field_table[0][tablefield][table][0][2]'] = 'Header 3';
$edit['field_table[0][tablefield][table][0][3]'] = 'Header 4';
$edit['field_table[0][tablefield][table][0][4]'] = 'Header 5';
$edit['field_table[0][tablefield][table][1][0]'] = 'Row 1-1';
$edit['field_table[0][tablefield][table][1][1]'] = 'Row 1-2';
$edit['field_table[0][tablefield][table][1][2]'] = 'Row 1-3';
$edit['field_table[0][tablefield][table][1][3]'] = 'Row 1-4';
$edit['field_table[0][tablefield][table][1][4]'] = 'Row 1-5';
$edit['field_table[0][tablefield][table][2][0]'] = 'Row 2-1';
$edit['field_table[0][tablefield][table][2][1]'] = 'Row 2-2';
$edit['field_table[0][tablefield][table][2][2]'] = 'Row 2-3';
$edit['field_table[0][tablefield][table][2][3]'] = 'Row 2-4';
$edit['field_table[0][tablefield][table][2][4]'] = 'Row 2-5';
$edit['field_table[0][tablefield][table][3][0]'] = 'Row 3-1';
$edit['field_table[0][tablefield][table][3][1]'] = 'Row 3-2';
$edit['field_table[0][tablefield][table][3][2]'] = 'Row 3-3';
$edit['field_table[0][tablefield][table][3][3]'] = 'Row 3-4';
$edit['field_table[0][tablefield][table][3][4]'] = 'Row 3-5';
$edit['field_table[0][tablefield][table][4][0]'] = 'Row 4-1';
$edit['field_table[0][tablefield][table][4][1]'] = 'Row 4-2';
$edit['field_table[0][tablefield][table][4][2]'] = 'Row 4-3';
$edit['field_table[0][tablefield][table][4][3]'] = 'Row 4-4';
$edit['field_table[0][tablefield][table][4][4]'] = 'Row 4-5';
$this->drupalPostForm(NULL, $edit, t('Save'));
// Start assertions.
$assert_session = $this->assertSession();
// Assert if the article has been created.
$assert_session->pageTextContains('Article Llamas are cool has been created.');
// Click on the export data to generate de CSV
$this->clickLink('Export Table Data');
// Assert if the CSV has been created.
$assert_session->statusCodeEquals(200);
$assert_session->responseHeaderContains('Content-Type', 'text/csv; charset=utf-8');
// Assert CSV content.
$assert_session->responseContains(
"\"Header 1\",\"Header 2\",\"Header 3\",\"Header 4\",\"Header 5\"\n".
"\"Row 1-1\",\"Row 1-2\",\"Row 1-3\",\"Row 1-4\",\"Row 1-5\"\n".
"\"Row 2-1\",\"Row 2-2\",\"Row 2-3\",\"Row 2-4\",\"Row 2-5\"\n".
"\"Row 3-1\",\"Row 3-2\",\"Row 3-3\",\"Row 3-4\",\"Row 3-5\"\n".
"\"Row 4-1\",\"Row 4-2\",\"Row 4-3\",\"Row 4-4\",\"Row 4-5\""
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment