Skip to content

Commit

Permalink
Merge pull request #9 from creative-commoners/pulls/1.0/handle-public…
Browse files Browse the repository at this point in the history
…-folder

FIX Generated PDF assets are now stored in the public assets folder if configured
  • Loading branch information
NightJar authored Mar 7, 2019
2 parents 5c834f1 + 382bba2 commit 16419c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Extensions/PdfExportExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CWP\PDFExport\Extensions;

use SilverStripe\Assets\File;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Versioned\Versioned;
Expand Down Expand Up @@ -49,8 +50,8 @@ public function getPdfFilename()
$baseName = sprintf('%s-%s', $this->owner->URLSegment, $this->owner->ID);

$folderPath = $this->owner->config()->get('generated_pdf_path');
if ($folderPath[0] != '/') {
$folderPath = BASE_PATH . '/' . $folderPath;
if ($folderPath[0] !== '/') {
$folderPath = File::join_paths(Director::publicFolder(), $folderPath);
}

return sprintf('%s/%s.pdf', $folderPath, $baseName);
Expand Down
28 changes: 22 additions & 6 deletions tests/Extensions/PdfExportExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CWP\CWP\PageTypes\BasePage;
use CWP\PDFExport\Extensions\PdfExportExtension;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;

Expand All @@ -26,14 +27,29 @@ protected function setUp()
->set(BasePage::class, 'generated_pdf_path', 'assets/_generated_pdfs');
}

public function testPdfFilename()
/**
* @param string $publicDir
* @param string $expected
* @dataProvider pdfFilenameProvider
*/
public function testPdfFilenameWithoutPublicDirectory($publicDir, $expected)
{
Director::config()->set('alternate_public_dir', $publicDir);

/** @var BasePage|PdfExportExtension $page $page */
$page = $this->objFromFixture(BasePage::class, 'test-page-one');
$this->assertContains(
'assets/_generated_pdfs/test-page-one-1.pdf',
$page->getPdfFilename(),
'Generated filename for PDF'
);
$this->assertContains($expected, $page->getPdfFilename());
}

/**
* @return array[]
*/
public function pdfFilenameProvider()
{
return [
'no public folder' => ['', 'assets/_generated_pdfs/test-page-one-1.pdf'],
'public folder' => ['public', 'public/assets/_generated_pdfs/test-page-one-1.pdf'],
];
}

public function testPdfLink()
Expand Down

0 comments on commit 16419c2

Please sign in to comment.