diff --git a/tests/unit/workflow/engine/classes/model/OutputDocumentTest.php b/tests/unit/workflow/engine/classes/model/OutputDocumentTest.php index 59329b216..e0187c3d5 100644 --- a/tests/unit/workflow/engine/classes/model/OutputDocumentTest.php +++ b/tests/unit/workflow/engine/classes/model/OutputDocumentTest.php @@ -2,6 +2,7 @@ namespace Tests\unit\workflow\engine\classes\model; +use Faker\Factory; use G; use OutputDocument; use ProcessMaker\Model\OutputDocument as OutputDocumentModel; @@ -14,6 +15,30 @@ use Tests\TestCase; */ class OutputDocumentTest extends TestCase { + var $faker = null; + /** + * OutputDocumentTest constructor. + * @param string $name + * @param array $data + * @param string $dataName + */ + public function __construct($name = null, array $data = [], $dataName = '') + { + // Faker instance + $this->faker = Factory::create(); + + // Check if the constant "K_PATH_FONTS" is defined, if is not defined we need to define + if (!defined('K_PATH_FONTS')) { + // Generate a new folder name + $folderName = $this->faker->word; + + // Define the path of the fonts, "K_PATH_FONTS" is a constant used by "TCPDF" library + define('K_PATH_FONTS', PATH_DATA . 'fonts' . PATH_SEP . $folderName . PATH_SEP); + } + + // Parent constructor + parent::__construct($name, $data, $dataName); + } /** * Review the generate pdf using TCPDF @@ -68,7 +93,216 @@ class OutputDocumentTest extends TestCase $properties ); $this->assertFileExists($pathOutput . $output->OUT_DOC_FILENAME . '.pdf'); - // Remove the shared folder - G::rm_dir($fonts); + } + + /** + * Test checkTcPdfFontsPath method + * + * @test + * @covers \OutputDocument::checkTcPdfFontsPath() + */ + public function it_should_test_check_tcpdf_fonts_path() + { + // Generate a new folder name + $folderName = $this->faker->word; + + // Check if the TCPDF fonts path exists, if not exists should be created and initialized + OutputDocument::checkTcPdfFontsPath($folderName); + + // Assertion + $this->assertDirectoryExists(K_PATH_FONTS); + } + + /** + * Test loadTcPdfFontsList method + * + * @test + * @covers \OutputDocument::loadTcPdfFontsList() + */ + public function it_should_test_load_tcpdf_fonts_list() + { + // Get fonts + $fonts = OutputDocument::loadTcPdfFontsList(); + + // Assertion + $this->assertTrue(is_array($fonts)); + } + + /** + * Test saveTcPdfFontsList method + * + * @test + * @covers \OutputDocument::saveTcPdfFontsList() + */ + public function it_should_test_save_tcpdf_fonts_list() + { + // Get fonts stored originally + $fontsOriginal = OutputDocument::loadTcPdfFontsList(); + + // Set variables needed + $fontFamily = $this->faker->word; + $font = [ + 'fileName' => "{$fontFamily}.ttf", + 'tcPdfFileName' => $fontFamily, + 'familyName' => $fontFamily, + 'inTinyMce' => true, + 'friendlyName' => $fontFamily, + 'properties' => '' + ]; + $fontsToSave = $fontsOriginal; + $fontsToSave[] = $font; + + // Check if TCPDF fonts paths exists + if (!file_exists(K_PATH_FONTS)) { + G::mk_dir(K_PATH_FONTS); + } + + // Save fonts + OutputDocument::saveTcPdfFontsList($fontsToSave); + + // Get fonts + $fontsModified = OutputDocument::loadTcPdfFontsList(); + + // Assertion + $this->assertTrue(count($fontsModified) > count($fontsOriginal)); + } + + /** + * Test addTcPdfFont method + * + * @test + * @covers \OutputDocument::addTcPdfFont() + */ + public function it_should_test_add_tcpdf_font() + { + // Set variables needed + $fontFamily = $this->faker->word; + $font = [ + 'fileName' => "{$fontFamily}.ttf", + 'tcPdfFileName' => $fontFamily, + 'familyName' => $fontFamily, + 'inTinyMce' => true, + 'friendlyName' => $fontFamily, + 'properties' => '' + ]; + + // Check if TCPDF fonts paths exists + if (!file_exists(K_PATH_FONTS)) { + G::mk_dir(K_PATH_FONTS); + } + + // Add new font + OutputDocument::addTcPdfFont($font); + + // Get fonts + $fonts = OutputDocument::loadTcPdfFontsList(); + + // Assertion + $this->assertArrayHasKey("{$fontFamily}.ttf", $fonts); + } + + /** + * Test existTcpdfFont method + * + * @test + * @covers \OutputDocument::existTcpdfFont() + */ + public function it_should_test_exist_tcpdf_font() + { + // Generate a fake family name + $fontFamily = $this->faker->word; + + // Add a new font + $font = [ + 'fileName' => "{$fontFamily}.ttf", + 'tcPdfFileName' => $fontFamily, + 'familyName' => $fontFamily, + 'inTinyMce' => true, + 'friendlyName' => $fontFamily, + 'properties' => '' + ]; + + // Check if TCPDF fonts paths exists + if (!file_exists(K_PATH_FONTS)) { + G::mk_dir(K_PATH_FONTS); + } + + // Add new font + OutputDocument::addTcPdfFont($font); + + // Assertion + $this->assertTrue(OutputDocument::existTcpdfFont("{$fontFamily}.ttf")); + } + + /** + * Test removeTcPdfFont method + * + * @test + * @covers \OutputDocument::removeTcPdfFont() + */ + public function it_should_test_remove_tcpdf_font() + { + // Generate a fake family name + $fontFamily = $this->faker->word; + + // Add a new font + $font = [ + 'fileName' => "{$fontFamily}.ttf", + 'tcPdfFileName' => $fontFamily, + 'familyName' => $fontFamily, + 'inTinyMce' => true, + 'friendlyName' => $fontFamily, + 'properties' => '' + ]; + + // Check if TCPDF fonts paths exists + if (!file_exists(K_PATH_FONTS)) { + G::mk_dir(K_PATH_FONTS); + } + + // Add new font + OutputDocument::addTcPdfFont($font); + + // Remove font + OutputDocument::removeTcPdfFont("{$fontFamily}.ttf"); + + // Assertion + $this->assertFalse(OutputDocument::existTcpdfFont("{$fontFamily}.ttf")); + } + + /** + * Test generateCssFile method + * + * @test + * @covers \OutputDocument::generateCssFile() + */ + public function it_should_test_generate_css_file() + { + // Set variables needed + $fontFamily = $this->faker->word; + $font = [ + 'fileName' => "{$fontFamily}.ttf", + 'tcPdfFileName' => $fontFamily, + 'familyName' => $fontFamily, + 'inTinyMce' => true, + 'friendlyName' => $fontFamily, + 'properties' => '' + ]; + $fontsToSave = ["{$fontFamily}.ttf" => $font]; + + // Check if TCPDF fonts paths exists + if (!file_exists(K_PATH_FONTS)) { + G::mk_dir(K_PATH_FONTS); + } + + // Save fonts + OutputDocument::saveTcPdfFontsList($fontsToSave); + + // Re-generate CSS file + OutputDocument::generateCssFile(); + + // Assertion + $cssContent = file_get_contents(K_PATH_FONTS . 'fonts.css'); + $this->assertTrue(strpos($cssContent, "{$fontFamily}.ttf") !== false); } } \ No newline at end of file diff --git a/workflow/engine/classes/model/OutputDocument.php b/workflow/engine/classes/model/OutputDocument.php index f1c3e21c9..32db194f6 100644 --- a/workflow/engine/classes/model/OutputDocument.php +++ b/workflow/engine/classes/model/OutputDocument.php @@ -1224,11 +1224,15 @@ class OutputDocument extends BaseOutputDocument /** * Check and prepare the fonts path used by TCPDF library + * + * @param string $folderName */ - public static function checkTcPdfFontsPath() + public static function checkTcPdfFontsPath($folderName = 'tcpdf') { - // Define the path of the fonts, "K_PATH_FONTS" is a constant used by "TCPDF" library - define('K_PATH_FONTS', PATH_DATA . 'fonts' . PATH_SEP . 'tcpdf' . PATH_SEP); + if (!defined('K_PATH_FONTS')) { + // Define the path of the fonts, "K_PATH_FONTS" is a constant used by "TCPDF" library + define('K_PATH_FONTS', PATH_DATA . 'fonts' . PATH_SEP . $folderName . PATH_SEP); + } // Check if already exists the path, if not exist we need to prepare the same if (!file_exists(K_PATH_FONTS)) { @@ -1335,7 +1339,7 @@ class OutputDocument extends BaseOutputDocument /** * Generate CSS with the fonts definition to be used by TinyMCE editor */ - private static function generateCssFile() + public static function generateCssFile() { // Initialize variables $template = "@font-face {font-family: @familyName;src: url(/fonts/font.php?file=@fileName) format('truetype');@properties}\n";