Skip to content
Snippets Groups Projects
Commit 6be57dfb authored by Scott Euser's avatar Scott Euser
Browse files

Issue #3459123 by scott_euser: Add basic automated test coverage

parent c603dc72
No related branches found
No related tags found
1 merge request!23Resolve #3459123 "Add basic tests"
Pipeline #376246 passed
name: Page to PDF Test
description: 'Test module for Page to PDF.'
type: module
core_version_requirement: ^10 || ^11
dependencies:
- drupal:page_to_pdf
<?php
namespace Drupal\page_to_pdf_test\Plugin\PdfGenerator;
use Drupal\Core\File\Exception\FileException;
use Drupal\page_to_pdf\Plugin\PdfGenerator\DoppioPdfGenerator;
/**
* Provides a Test PDF Generator Service.
*
* @PdfGenerator(
* id = "test_pdf",
* label = @Translation("Test PDF"),
* )
*/
class TestPdfGenerator extends DoppioPdfGenerator {
/**
* {@inheritdoc}
*/
public function generatePdf(string $page_url, string $output_file_uri, array $options = []): void {
try {
// Instead of calling the actual API, directly save the mock PDF data.
$this->saveMockPdfToFile($output_file_uri);
}
catch (\Exception $e) {
$this->logger
->error($this->t("Mock PDF generation failed with exception: '@error_message'.", ["@error_message" => $e->getMessage()]));
}
}
/**
* Save mock PDF data to the given file.
*
* @param string $path
* Destination file path.
*/
protected function saveMockPdfToFile(string $path): void {
// Mock PDF content.
$pdf_data = <<<PDF
%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Count 1 /Kids [3 0 R] >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R >>
endobj
4 0 obj
<< /Length 55 >>
stream
BT
/F1 24 Tf
100 700 Td
(This is a mock PDF file.) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000074 00000 n
0000000123 00000 n
0000000220 00000 n
0000000321 00000 n
trailer
<< /Size 6 /Root 1 0 R >>
startxref
390
%%EOF
PDF;
// Create directory.
$directory = $this->fileSystem->dirname($path);
if (!$this->fileSystem->prepareDirectory($directory)) {
if (!$this->fileSystem->mkdir($directory)) {
throw new FileException(\sprintf('Could not create the directory %s.', $directory));
}
}
// Save the file.
if (file_exists($path) && !is_writable($path)) {
throw new FileException(\sprintf('The file %s is not writable.', $path));
}
if (!$this->fileSystem->saveData($pdf_data, $path)) {
throw new FileException(\sprintf('The file %s could not be saved.', $path));
}
}
}
<?php
namespace Drupal\page_to_pdf_test\Plugin\PuppeteerService;
use Drupal\page_to_pdf\Plugin\PuppeteerServiceBase;
/**
* Provides a Test Puppeteer Service.
*
* @PuppeteerService(
* id = "test_pdf",
* label = @Translation("Test PDF"),
* )
*/
class TestPuppeteerService extends PuppeteerServiceBase {
}
<?php
namespace Drupal\Tests\page_to_pdf\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Contains Page to PDF UI functional tests.
*
* @group page_to_pdf
*/
class PageToPdfUiTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'page_to_pdf',
'page_to_pdf_test',
'node',
'user',
'system',
'media',
'file',
'field_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A user with permission to bypass access content.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create Basic page and Article node types.
if ($this->profile != 'standard') {
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$this->adminUser = $this->drupalCreateUser([
'access administration pages',
'administer content types',
'access content overview',
'administer nodes',
'administer node fields',
'bypass node access',
'administer media types',
]);
}
/**
* Tests setting up Page to PDF for Article.
*/
public function testPageToPdfSetup(): void {
$this->drupalLogin($this->adminUser);
// Expect media reference to not exist yet.
$this->drupalGet('admin/structure/types/manage/article');
$this->submitForm([
'pdf_enable' => TRUE,
], 'Save');
$this->assertSession()->pageTextContains('"Media Reference field" is required');
// Create media type.
$this->drupalGet('admin/structure/media/add');
$this->submitForm([
'label' => 'PDF',
'id' => 'pdf',
'source' => 'file',
], 'Save');
$this->getSession()->getPage()->pressButton('Save');
// Create a media file field on the article.
$this->drupalGet('admin/structure/types/manage/article/fields/add-field');
$this->submitForm([
'new_storage_type' => 'field_ui:entity_reference:media',
], 'Continue');
$this->submitForm([
'label' => 'PDF',
'field_name' => 'pdf',
], 'Continue');
$this->submitForm([], 'Save settings');
$this->submitForm([
'settings[handler_settings][target_bundles][pdf]' => 'pdf',
], 'Save settings');
// Re-attempt saving settings.
$this->drupalGet('admin/structure/types/manage/article');
$this->submitForm([
'pdf_enable' => TRUE,
'pdf_field_save' => 'field_pdf',
'pdf_view_modes' => 'teaser',
'pdf_plugin' => 'test_pdf',
], 'Save');
// Create a node, expect to see the generate button.
$this->drupalGet('node/add/article');
$this->assertSession()->buttonExists('Save and generate PDF');
$this->submitForm([
'title[0][value]' => 'Test 1',
], 'Save');
// Check there is a PDF preview and it correctly renders the teaser view
// mode.
$this->drupalGet('node/1/pdf');
$session = $this->assertSession();
$session->pageTextContains('Test 1');
$session->pageTextContains('Read more');
// Test generating the PDF.
$this->drupalGet('node/1/edit');
$this->submitForm([], 'Save and generate PDF');
$this->assertSession()->pageTextContains('The PDF file was successfully generated.');
$this->getSession()->getPage()->findLink('test-1-(1).pdf')->click();
$this->assertSession()->pageTextContains('This is a mock PDF file.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment