From 9626dce2fa95eeeff67510af6ca341b9c8050803 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 19 Mar 2019 14:00:23 +0100 Subject: [PATCH 1/2] fixed --- src/Utility/LinkScanner.php | 4 ++-- tests/TestCase/Command/LinkScannerCommandTest.php | 8 ++++---- tests/TestCase/Utility/LinkScannerTest.php | 10 +++++----- tests/bootstrap.php | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index 0d4645c..166714b 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -61,7 +61,7 @@ class LinkScanner implements Serializable 'followRedirects' => false, 'maxDepth' => 0, 'lockFile' => true, - 'target' => TMP, + 'target' => TMP . 'cakephp-link-scanner', ]; /** @@ -98,7 +98,7 @@ class LinkScanner implements Serializable * Lock file path * @var string */ - protected $lockFile = TMP . 'link_scanner_lock_file'; + protected $lockFile = TMP . 'cakephp-link-scanner' . DS . 'link_scanner_lock_file'; /** * Start time diff --git a/tests/TestCase/Command/LinkScannerCommandTest.php b/tests/TestCase/Command/LinkScannerCommandTest.php index 0a47470..d2c266b 100644 --- a/tests/TestCase/Command/LinkScannerCommandTest.php +++ b/tests/TestCase/Command/LinkScannerCommandTest.php @@ -85,7 +85,7 @@ public function testScan() 'followRedirects' => false, 'maxDepth' => 1, 'lockFile' => true, - 'target' => TMP, + 'target' => TMP . 'cakephp-link-scanner', ], $this->LinkScanner->getConfig()); $this->assertOutputRegExp(sprintf('/Scan started for %s at [\d\-]+\s[\d\:]+/', preg_quote($this->fullBaseUrl, '/'))); @@ -134,7 +134,7 @@ public function testScanCacheEnabled() */ public function testScanParams() { - touch(LINK_SCANNER_LOCK_FILE); + touch($this->Command->LinkScanner->lockFile); $params = [ '--export', '--force', @@ -151,14 +151,14 @@ public function testScanParams() 'followRedirects' => false, 'maxDepth' => 2, 'lockFile' => false, - 'target' => TMP, + 'target' => TMP . 'cakephp-link-scanner', ]; $expectedDuration = Cache::getConfig('LinkScanner')['duration']; $expectedFilename = $this->LinkScanner->getConfig('target') . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; $this->assertEquals($expectedConfig, $this->LinkScanner->getConfig()); $this->assertEquals(15, $this->LinkScanner->Client->getConfig('timeout')); - $this->assertFileNotExists(LINK_SCANNER_LOCK_FILE); + $this->assertFileNotExists($this->LinkScanner->lockFile); $this->assertFileExists($expectedFilename); $this->assertEventFired('LinkScanner.resultsExported', $this->LinkScanner->getEventManager()); $this->assertOutputRegExp(sprintf('/Scan started for %s/', preg_quote($this->fullBaseUrl, '/'))); diff --git a/tests/TestCase/Utility/LinkScannerTest.php b/tests/TestCase/Utility/LinkScannerTest.php index 0287bc1..a331816 100644 --- a/tests/TestCase/Utility/LinkScannerTest.php +++ b/tests/TestCase/Utility/LinkScannerTest.php @@ -147,8 +147,8 @@ public function testExport() //Filename can be `null`, relative or absolute foreach ([ - null => $this->LinkScanner->getConfig('target') . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime, - 'example' => $this->LinkScanner->getConfig('target') . 'example', + null => $this->LinkScanner->getConfig('target') . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime, + 'example' => $this->LinkScanner->getConfig('target') . DS . 'example', TMP . 'example' => TMP . 'example', ] as $filenameWhereToExport => $expectedFilename) { $result = $this->LinkScanner->export($filenameWhereToExport); @@ -195,7 +195,7 @@ public function testImport() 'followRedirects' => false, 'maxDepth' => 1, 'lockFile' => true, - 'target' => TMP, + 'target' => TMP . 'cakephp-link-scanner', ], $result->getConfig()); $this->assertEquals(100, $result->Client->getConfig('timeout')); @@ -305,8 +305,8 @@ public function testScan() //The lock file alread exists $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Lock file `' . LINK_SCANNER_LOCK_FILE . '` already exists, maybe a scan is already in progress. If not, remove it manually'); - file_put_contents(LINK_SCANNER_LOCK_FILE, null); + $this->expectExceptionMessage('Lock file `' . $LinkScanner->lockFile . '` already exists, maybe a scan is already in progress. If not, remove it manually'); + file_put_contents($LinkScanner->lockFile, null); (new LinkScanner)->scan(); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4e62152..a97b399 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -33,13 +33,14 @@ define('APP_DIR', 'TestApp'); define('WEBROOT_DIR', 'webroot'); define('WWW_ROOT', APP . 'webroot' . DS); -define('TMP', sys_get_temp_dir() . DS . 'cakephp-link-scanner' . DS); +define('TMP', sys_get_temp_dir() . DS); define('CACHE', TMP . 'cache' . DS); define('CONFIG', APP . 'config' . DS); define('LOGS', TMP . 'logs' . DS); define('SESSIONS', TMP . 'sessions' . DS); @mkdir(TMP); +@mkdir(TMP . 'cakephp-link-scanner'); @mkdir(LOGS); @mkdir(SESSIONS); @mkdir(CACHE); @@ -87,7 +88,6 @@ ], ]); -define('LINK_SCANNER_LOCK_FILE', TMP . 'link_scanner_lock_file'); Configure::write('pluginsToLoad', ['LinkScanner']); loadPHPUnitAliases(); From e208e2e1e8a5e89a9ee33c64b0741b2e00b1ce11 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 19 Mar 2019 14:17:54 +0100 Subject: [PATCH 2/2] fixed code --- src/Utility/LinkScanner.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index 166714b..e7e419e 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -17,6 +17,7 @@ use Cake\Core\InstanceConfigTrait; use Cake\Event\EventDispatcherTrait; use Cake\Event\EventList; +use Cake\Filesystem\File; use Cake\Filesystem\Folder; use Cake\Http\Client; use Cake\Http\Client\Response; @@ -124,7 +125,7 @@ public function __construct($fullBaseUrl = null, $Client = null, $ResultScan = n $this->Client = $Client ?: new Client(['redirect' => true]); $this->ResultScan = $ResultScan ?: new ResultScan; - $this->setFullBaseUrl($fullBaseUrl ?: Configure::read('App.fullBaseUrl', 'http://localhost')); + $this->setFullBaseUrl($fullBaseUrl ?: (Configure::read('App.fullBaseUrl') ?: 'http://localhost')); } /** @@ -145,13 +146,16 @@ public function __get($name) */ protected function _createLockFile() { - is_true_or_fail(!$this->getConfig('lockFile') || !file_exists($this->lockFile), __d( + $lockFile = new File($this->lockFile); + is_true_or_fail(!$this->getConfig('lockFile') || !$lockFile->exists(), __d( 'link-scanner', 'Lock file `{0}` already exists, maybe a scan is already in progress. If not, remove it manually', $this->lockFile ), RuntimeException::class); - return $this->getConfig('lockFile') ? touch($this->lockFile) !== false : true; + ; + + return $this->getConfig('lockFile') ? $lockFile->create() !== false : true; } /**