From cc2fc13c947179b6d966bc901b448c0d4916a1dc Mon Sep 17 00:00:00 2001 From: Samuel Nwaokoro Date: Fri, 24 May 2024 17:32:39 +0100 Subject: [PATCH 1/7] provision for file exclusions and unit tests --- composer.json | 2 +- src/Traits/GeneratePotTrait.php | 39 +++++++ tests/cases/GenerateLangExclusionsTest.php | 115 +++++++++++++++++++++ 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 tests/cases/GenerateLangExclusionsTest.php diff --git a/composer.json b/composer.json index a3038cc..601f267 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "10quality/gettext-wp-scanner": "^1.0" }, "require-dev": { - "phpunit/phpunit": "9.*" + "phpunit/phpunit": "9.6.x-dev" }, "autoload": { "psr-4": { diff --git a/src/Traits/GeneratePotTrait.php b/src/Traits/GeneratePotTrait.php index 180f3b6..50a50d6 100644 --- a/src/Traits/GeneratePotTrait.php +++ b/src/Traits/GeneratePotTrait.php @@ -48,18 +48,28 @@ protected function generatePot($lang = 'en') Translations::create($domain) ); foreach (glob($this->rootPath.'*.php') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } foreach (glob($this->getAppPath().'*.php') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } foreach (glob($this->getAppPath().'**/*.php') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } foreach (glob($this->getViewsPath().'*.php') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } foreach (glob($this->getViewsPath().'**/*.php') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } $scannedTranslations = $scanner->getTranslations(); @@ -70,9 +80,13 @@ protected function generatePot($lang = 'en') Translations::create($domain) ); foreach (glob($this->getAssetsPath().'js/*.js') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } foreach (glob($this->getAssetsPath().'js/**/*.js') as $file) { + if ($this->isFileToLocalizeExcluded($file)) + continue; $scanner->scanFile($file); } $scannedTranslations = $scanner->getTranslations(); @@ -98,6 +112,31 @@ protected function generatePot($lang = 'en') } } + /** + * Exclude files from being scanned. + * @since 1.1.18 + * + * @param string $file + * + * @return bool + */ + protected function isFileToLocalizeExcluded( $file ) { + $should_exclude = false; + $exclusions = array_key_exists( 'translations', $this->config['localize'] ) + && array_key_exists( 'file_exclusions', $this->config['localize']['translations'] ) + && !empty( $this->config['localize']['translations']['file_exclusions'] ) + && is_array( $this->config['localize']['translations']['file_exclusions'] ) + ? $this->config['localize']['translations']['file_exclusions'] + : []; + foreach( $exclusions as $exclusion ) { + if ( strpos( $file, $exclusion ) !== false ) { + $should_exclude = true; + break; + } + } + return $should_exclude; + } + /** * Generate PO file. * @since 1.1.0 diff --git a/tests/cases/GenerateLangExclusionsTest.php b/tests/cases/GenerateLangExclusionsTest.php new file mode 100644 index 0000000..1b56c4c --- /dev/null +++ b/tests/cases/GenerateLangExclusionsTest.php @@ -0,0 +1,115 @@ + + * @copyright 10Quality + * @license MIT + * @package WPMVC\Commands + * @version 1.1.17 + */ +class GenerateLangExclusionsTest extends WpmvcAyucoTestCase +{ + /** + * Temporary hold for bootstrap config. + */ + protected $configBackup; + /** + * paths to be unlinked during Teardown. + */ + protected $path = [ + FRAMEWORK_PATH.'/environment/assets/lang/', + FRAMEWORK_PATH.'/environment/assets/views/', + FRAMEWORK_PATH.'/environment/assets/js/', + FRAMEWORK_PATH.'/environment/assets/', + ]; + /** + * Run before tests. + */ + public function setUp(): void + { + // Make backup of current config + $config = TESTING_PATH.'/app/Config/app.php'; + $this->configBackup = file_get_contents($config); + // Make other files + if (!is_dir(TESTING_PATH.'/assets/views/')) + mkdir(TESTING_PATH.'/assets/views/', 0777, true); + if (!is_file(TESTING_PATH.'/assets/views/localize.php')) + file_put_contents(TESTING_PATH.'/assets/views/localize.php', ''); + if (!is_file(TESTING_PATH.'/assets/views/excluded.php')) + file_put_contents(TESTING_PATH.'/assets/views/excluded.php', ''); + if (!is_dir(TESTING_PATH.'/assets/js/')) + mkdir(TESTING_PATH.'/assets/js/', 0777, true); + if (!is_file(TESTING_PATH.'/assets/js/excluded.js')) + file_put_contents(TESTING_PATH.'/assets/js/excluded.js', '__( \'Excluded text\', \'my-app\' );'); + // Override config + file_put_contents($config, ' \'MyApp\', + \'type\' => \'theme\', + \'version\' => \'1.0.0\', + \'author\' => \'Developer \', + \'paths\' => [ + \'base\' => __DIR__ . \'/../\', + \'controllers\' => __DIR__ . \'/../Controllers/\', + \'views\' => __DIR__ . \'/../../assets/views/\', + ], + \'localize\' => [ + \'textdomain\' => \'my-app\', + \'path\' => __DIR__ . \'/../../assets/lang/\', + \'translations\' => [ + \'file_exclusions\' => [\'excluded.php\', \'excluded.js\'], + ], + ], + ];' + ); + } + /** + * Restore bootstrap config. + * @since + */ + public function tearDown(): void + { + parent::tearDown(); + file_put_contents(TESTING_PATH.'/app/Config/app.php', $this->configBackup); + } + /** + * Test when configuration is set. + * @group pot + * @group localization + */ + public function testPOTExclusion() + { + // Prepare + $loader = new PoLoader; + $filename = TESTING_PATH.'/assets/lang/my-app.pot'; + // Execute + $execution = exec('php '.WPMVC_AYUCO.' generate pot'); + $translations = $loader->loadFile($filename); + // Assert + $this->assertEquals('POT file generated!', $execution); + $this->assertFileExists($filename); + $this->assertCount(1, $translations); + } + /** + * Test when configuration is set. + * @group po + * @group localization + */ + public function testPOExclusion() + { + // Prepare + $loader = new PoLoader; + $filename = TESTING_PATH.'/assets/lang/my-app-en_US.po'; + // Execute + $execution = exec('php '.WPMVC_AYUCO.' generate po:en_US'); + $translations = $loader->loadFile($filename); + // Assert + $this->assertEquals('PO:en_US file generated!', $execution); + $this->assertFileExists($filename); + $this->assertCount(1, $translations); + } +} \ No newline at end of file From 28dcf1d9ea262ab427c8711bf2ac7de0fdca9ecf Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 24 May 2024 18:04:56 +0100 Subject: [PATCH 2/7] Fix indentation --- tests/cases/GenerateLangExclusionsTest.php | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/cases/GenerateLangExclusionsTest.php b/tests/cases/GenerateLangExclusionsTest.php index 1b56c4c..d2ddbb9 100644 --- a/tests/cases/GenerateLangExclusionsTest.php +++ b/tests/cases/GenerateLangExclusionsTest.php @@ -13,19 +13,19 @@ */ class GenerateLangExclusionsTest extends WpmvcAyucoTestCase { - /** - * Temporary hold for bootstrap config. - */ + /** + * Temporary hold for bootstrap config. + */ protected $configBackup; - /** - * paths to be unlinked during Teardown. - */ - protected $path = [ - FRAMEWORK_PATH.'/environment/assets/lang/', - FRAMEWORK_PATH.'/environment/assets/views/', - FRAMEWORK_PATH.'/environment/assets/js/', - FRAMEWORK_PATH.'/environment/assets/', - ]; + /** + * paths to be unlinked during Teardown. + */ + protected $path = [ + FRAMEWORK_PATH.'/environment/assets/lang/', + FRAMEWORK_PATH.'/environment/assets/views/', + FRAMEWORK_PATH.'/environment/assets/js/', + FRAMEWORK_PATH.'/environment/assets/', + ]; /** * Run before tests. */ @@ -112,4 +112,4 @@ public function testPOExclusion() $this->assertFileExists($filename); $this->assertCount(1, $translations); } -} \ No newline at end of file +} From 9d1b82b968a9b390bf89a627445fb81889ae1146 Mon Sep 17 00:00:00 2001 From: Samuel Nwaokoro Date: Fri, 24 May 2024 18:07:38 +0100 Subject: [PATCH 3/7] Fix spacing --- tests/cases/GenerateLangExclusionsTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/cases/GenerateLangExclusionsTest.php b/tests/cases/GenerateLangExclusionsTest.php index d2ddbb9..1f87a6b 100644 --- a/tests/cases/GenerateLangExclusionsTest.php +++ b/tests/cases/GenerateLangExclusionsTest.php @@ -13,13 +13,13 @@ */ class GenerateLangExclusionsTest extends WpmvcAyucoTestCase { - /** - * Temporary hold for bootstrap config. - */ + /** + * Temporary hold for bootstrap config. + */ protected $configBackup; /** - * paths to be unlinked during Teardown. - */ + * paths to be unlinked during Teardown. + */ protected $path = [ FRAMEWORK_PATH.'/environment/assets/lang/', FRAMEWORK_PATH.'/environment/assets/views/', From 9c81484b3a0a72d46aae71589f56c4ea9e0c17bb Mon Sep 17 00:00:00 2001 From: Samuel Nwaokoro Date: Fri, 24 May 2024 21:29:13 +0100 Subject: [PATCH 4/7] Fixed indentations, spacing and code refactor --- composer.json | 2 +- src/Traits/GeneratePotTrait.php | 28 +++++++++++----------- tests/cases/GenerateLangExclusionsTest.php | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 601f267..a3038cc 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "10quality/gettext-wp-scanner": "^1.0" }, "require-dev": { - "phpunit/phpunit": "9.6.x-dev" + "phpunit/phpunit": "9.*" }, "autoload": { "psr-4": { diff --git a/src/Traits/GeneratePotTrait.php b/src/Traits/GeneratePotTrait.php index 50a50d6..8612594 100644 --- a/src/Traits/GeneratePotTrait.php +++ b/src/Traits/GeneratePotTrait.php @@ -19,7 +19,7 @@ * @copyright 10Quality * @license MIT * @package WPMVC\Commands - * @version 1.1.17 + * @version 1.1.18 */ trait GeneratePotTrait { @@ -120,21 +120,21 @@ protected function generatePot($lang = 'en') * * @return bool */ - protected function isFileToLocalizeExcluded( $file ) { - $should_exclude = false; - $exclusions = array_key_exists( 'translations', $this->config['localize'] ) - && array_key_exists( 'file_exclusions', $this->config['localize']['translations'] ) - && !empty( $this->config['localize']['translations']['file_exclusions'] ) - && is_array( $this->config['localize']['translations']['file_exclusions'] ) + protected function isFileToLocalizeExcluded($file) + { + $exclusions = array_key_exists('localize',$this->config) + && array_key_exists('translations',$this->config['localize']) + && array_key_exists('file_exclusions',$this->config['localize']['translations']) + && !empty($this->config['localize']['translations']['file_exclusions']) + && is_array($this->config['localize']['translations']['file_exclusions']) ? $this->config['localize']['translations']['file_exclusions'] - : []; - foreach( $exclusions as $exclusion ) { - if ( strpos( $file, $exclusion ) !== false ) { - $should_exclude = true; - break; + : null; + if (!empty($exclusions)) + foreach($exclusions as $exclusion) { + if (strpos($file,$exclusion) !== false) + return true; } - } - return $should_exclude; + return false; } /** diff --git a/tests/cases/GenerateLangExclusionsTest.php b/tests/cases/GenerateLangExclusionsTest.php index 1f87a6b..ccfa3a0 100644 --- a/tests/cases/GenerateLangExclusionsTest.php +++ b/tests/cases/GenerateLangExclusionsTest.php @@ -9,7 +9,7 @@ * @copyright 10Quality * @license MIT * @package WPMVC\Commands - * @version 1.1.17 + * @version 1.1.18 */ class GenerateLangExclusionsTest extends WpmvcAyucoTestCase { From 95c2177bc8a7e90438fd03f96889a6192abec120 Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 24 May 2024 21:56:25 +0100 Subject: [PATCH 5/7] Fixed indentation on git after push From 4374d58b19d680c6233d84c30eadeec7541f0cac Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 24 May 2024 21:58:05 +0100 Subject: [PATCH 6/7] Indent entire file to 4 spaces. From 25067599ba2cfbaad75265181318b4c54cc67b9c Mon Sep 17 00:00:00 2001 From: Samuel Nwaokoro Date: Fri, 24 May 2024 22:11:09 +0100 Subject: [PATCH 7/7] Convert indentation to spaces --- tests/cases/GenerateLangExclusionsTest.php | 76 +++++++++++----------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/tests/cases/GenerateLangExclusionsTest.php b/tests/cases/GenerateLangExclusionsTest.php index ccfa3a0..00bedc6 100644 --- a/tests/cases/GenerateLangExclusionsTest.php +++ b/tests/cases/GenerateLangExclusionsTest.php @@ -16,25 +16,25 @@ class GenerateLangExclusionsTest extends WpmvcAyucoTestCase /** * Temporary hold for bootstrap config. */ - protected $configBackup; - /** + protected $configBackup; + /** * paths to be unlinked during Teardown. */ - protected $path = [ - FRAMEWORK_PATH.'/environment/assets/lang/', - FRAMEWORK_PATH.'/environment/assets/views/', - FRAMEWORK_PATH.'/environment/assets/js/', - FRAMEWORK_PATH.'/environment/assets/', - ]; + protected $path = [ + FRAMEWORK_PATH.'/environment/assets/lang/', + FRAMEWORK_PATH.'/environment/assets/views/', + FRAMEWORK_PATH.'/environment/assets/js/', + FRAMEWORK_PATH.'/environment/assets/', + ]; /** * Run before tests. */ public function setUp(): void { - // Make backup of current config - $config = TESTING_PATH.'/app/Config/app.php'; - $this->configBackup = file_get_contents($config); - // Make other files + // Make backup of current config + $config = TESTING_PATH.'/app/Config/app.php'; + $this->configBackup = file_get_contents($config); + // Make other files if (!is_dir(TESTING_PATH.'/assets/views/')) mkdir(TESTING_PATH.'/assets/views/', 0777, true); if (!is_file(TESTING_PATH.'/assets/views/localize.php')) @@ -45,43 +45,43 @@ public function setUp(): void mkdir(TESTING_PATH.'/assets/js/', 0777, true); if (!is_file(TESTING_PATH.'/assets/js/excluded.js')) file_put_contents(TESTING_PATH.'/assets/js/excluded.js', '__( \'Excluded text\', \'my-app\' );'); - // Override config - file_put_contents($config, ' \'MyApp\', - \'type\' => \'theme\', - \'version\' => \'1.0.0\', - \'author\' => \'Developer \', - \'paths\' => [ - \'base\' => __DIR__ . \'/../\', - \'controllers\' => __DIR__ . \'/../Controllers/\', - \'views\' => __DIR__ . \'/../../assets/views/\', - ], - \'localize\' => [ - \'textdomain\' => \'my-app\', - \'path\' => __DIR__ . \'/../../assets/lang/\', + // Override config + file_put_contents($config, ' \'MyApp\', + \'type\' => \'theme\', + \'version\' => \'1.0.0\', + \'author\' => \'Developer \', + \'paths\' => [ + \'base\' => __DIR__ . \'/../\', + \'controllers\' => __DIR__ . \'/../Controllers/\', + \'views\' => __DIR__ . \'/../../assets/views/\', + ], + \'localize\' => [ + \'textdomain\' => \'my-app\', + \'path\' => __DIR__ . \'/../../assets/lang/\', \'translations\' => [ \'file_exclusions\' => [\'excluded.php\', \'excluded.js\'], ], - ], - ];' - ); + ], + ];' + ); } - /** + /** * Restore bootstrap config. * @since */ - public function tearDown(): void - { - parent::tearDown(); - file_put_contents(TESTING_PATH.'/app/Config/app.php', $this->configBackup); - } + public function tearDown(): void + { + parent::tearDown(); + file_put_contents(TESTING_PATH.'/app/Config/app.php', $this->configBackup); + } /** * Test when configuration is set. * @group pot * @group localization */ - public function testPOTExclusion() + public function testPOTExclusion() { // Prepare $loader = new PoLoader; @@ -99,7 +99,7 @@ public function testPOTExclusion() * @group po * @group localization */ - public function testPOExclusion() + public function testPOExclusion() { // Prepare $loader = new PoLoader;