Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #2773 bc test case #2799

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions plugins/baser-core/src/TestSuite/BcTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use BaserCore\Service\BcDatabaseService;
use BaserCore\Utility\BcApiUtil;
use BaserCore\Utility\BcContainerTrait;
use BaserCore\Utility\BcFolder;
use BaserCore\Utility\BcUtil;
use BcBlog\ServiceProvider\BcBlogServiceProvider;
use BcContentLink\ServiceProvider\BcContentLinkServiceProvider;
Expand All @@ -32,7 +33,6 @@
use Cake\Datasource\ConnectionManager;
use Cake\Event\EventListenerInterface;
use Cake\Event\EventManager;
use Cake\Filesystem\Folder;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\ORM\TableRegistry;
Expand All @@ -46,6 +46,7 @@
use BaserCore\Annotation\Checked;
use Cake\Utility\Inflector;
use CakephpTestSuiteLight\Fixture\TruncateDirtyTables;
use Couchbase\LookupGetSpec;
use ReflectionClass;
use BaserCore\Utility\BcContainer;
use BaserCore\ServiceProvider\BcServiceProvider;
Expand Down Expand Up @@ -424,9 +425,10 @@ public function resetEvent()
*/
public static function tearDownAfterClass(): void
{
$folder = new Folder();
$folder->chmod(LOGS, 0777);
$folder->chmod(TMP, 0777);
$folder = new BcFolder(LOGS);
$folder->chmod( 0777);
$folder = new BcFolder(TMP);
$folder->chmod(0777);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions plugins/baser-core/src/Utility/BcFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function write($data)
return (bool) file_put_contents($this->path, $data);
}

<<<<<<< HEAD
/**
* ファイルを削除
* @return bool
Expand All @@ -113,6 +112,4 @@ public function delete()
}
return unlink($this->path);
}
=======
>>>>>>> dev-5
}
1 change: 0 additions & 1 deletion plugins/baser-core/src/Utility/BcFileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace BaserCore\Utility;

use ArrayObject;
use Cake\Filesystem\File;
use Cake\Http\Session;
use Cake\ORM\Entity;
use Cake\ORM\Table;
Expand Down
160 changes: 160 additions & 0 deletions plugins/baser-core/src/Utility/BcFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use BaserCore\Annotation\UnitTest;
use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
use Exception;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
* Class BcFolder
Expand All @@ -27,6 +31,21 @@ class BcFolder
*/
private string $path;

public int $mode = 0755;
/**
* Holds messages from last method.
*
* @var array
*/
protected array $_messages = [];

/**
* Holds errors from last method.
*
* @var array
*/
protected array $_errors = [];

/**
* Constructor
* @param string $path
Expand Down Expand Up @@ -155,4 +174,145 @@ public function delete()
return true;
}

/**
* ディレクトリ構造のモードを再帰的に変更します。これにはファイルのモードも変更することが含まれます。
* @checked
* @noTodo
* @unitTest
*/
public function chmod(?int $mode = null, bool $recursive = true, array $exceptions = []): bool
{
$path = $this->path;
if (!$mode) {
$mode = $this->mode;
}

if ($recursive === false && is_dir($path)) {
// phpcs:disable
if (@chmod($path, intval($mode, 8))) {
// phpcs:enable
$this->_messages[] = sprintf('%s changed to %s', $path, $mode);

return true;
}

$this->_errors[] = sprintf('%s NOT changed to %s', $path, $mode);

return false;
}

if (is_dir($path)) {
$paths = $this->tree($path);

foreach ($paths as $type) {
foreach ($type as $fullpath) {
$check = explode(DIRECTORY_SEPARATOR, $fullpath);
$count = count($check);

if (in_array($check[$count - 1], $exceptions, true)) {
continue;
}

// phpcs:disable
if (@chmod($fullpath, intval($mode, 8))) {
// phpcs:enable
$this->_messages[] = sprintf('%s changed to %s', $fullpath, $mode);
} else {
$this->_errors[] = sprintf('%s NOT changed to %s', $fullpath, $mode);
}
}
}

if (empty($this->_errors)) {
return true;
}
}

return false;
}

/**
* 各ディレクトリ内のネストされたディレクトリとファイルの配列を返す
* @checked
* @noTodo
* @unitTest
*/
public function tree(?string $path = null, $exceptions = false, ?string $type = null): array
{
if (!$path) {
$path = $this->path;
}
$files = [];
$directories = [$path];

if (is_array($exceptions)) {
$exceptions = array_flip($exceptions);
}
$skipHidden = false;
if ($exceptions === true) {
$skipHidden = true;
} elseif (isset($exceptions['.'])) {
$skipHidden = true;
unset($exceptions['.']);
}

try {
$directory = new RecursiveDirectoryIterator(
$path,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF
);
$iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
} catch (Exception $e) {
unset($directory, $iterator);

if ($type === null) {
return [[], []];
}

return [];
}

/**
* @var string $itemPath
* @var RecursiveDirectoryIterator $fsIterator
*/
foreach ($iterator as $itemPath => $fsIterator) {
if ($skipHidden) {
$subPathName = $fsIterator->getSubPathname();
if ($subPathName[0] === '.' || str_contains($subPathName, DIRECTORY_SEPARATOR . '.')) {
unset($fsIterator);
continue;
}
}
/** @var \FilesystemIterator $item */
$item = $fsIterator->current();
if (!empty($exceptions) && isset($exceptions[$item->getFilename()])) {
unset($fsIterator, $item);
continue;
}

if ($item->isFile()) {
$files[] = $itemPath;
} elseif ($item->isDir() && !$item->isDot()) {
$directories[] = $itemPath;
}

// inner iterators need to be unset too in order for locks on parents to be released
unset($fsIterator, $item);
}

// unsetting iterators helps to release possible locks in certain environments,
// which could otherwise make `rmdir()` fail
unset($directory, $iterator);

if ($type === null) {
return [$directories, $files];
}
if ($type === 'dir') {
return $directories;
}

return $files;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use BaserCore\Database\Schema\BcSchema;
use BaserCore\TestSuite\BcTestCase;
use Cake\Filesystem\File;
use BaserCore\Utility\BcFile;

/**
* Class BcSchemaTest
Expand Down Expand Up @@ -47,7 +47,8 @@ public function setUp(): void
$path = TMP . 'schema' . DS;
$schemaName = 'UserActionsSchema';
$schemaFilePath = $path . $schemaName . '.php';
$this->schemaFile = new File($schemaFilePath, true);
$this->schemaFile = new BcFile($schemaFilePath);
$this->schemaFile->create();
$table = 'user_actions';
// スキーマファイルを生成
$this->schemaFile->write("<?php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use BaserCore\Database\Schema\BcSchema;
use BaserCore\Utility\BcContainer;
use BaserCore\Utility\BcFile;
use BaserCore\View\Helper\BcFormHelper;
use Cake\Event\Event;
use Cake\Event\EventManager;
Expand All @@ -27,7 +28,6 @@
use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
use BaserCore\Annotation\UnitTest;
use Cake\Filesystem\File;

/**
* BaserCore\TestSuite\BcTestCase
Expand Down Expand Up @@ -286,7 +286,7 @@ public function testGetPrivateProperty()
{
$className = 'DummyClass';
$filePath = TMP . $className . '.php';
$file = new File($filePath, true);
$file = new BcFile($filePath);
// DummyClassファイルを作成する
$file->write("<?php
class $className
Expand Down
34 changes: 34 additions & 0 deletions plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,38 @@ public function testDelete()
$this->assertFalse(is_dir($parent));
}

/**
* test chmod
*/
public function test_chmod()
{
$path = TMP_TESTS . 'test' . DS . 'chmod';
$folder = new BcFolder($path);
$folder->create();
$folder->chmod(0777);
$this->assertEquals('0777', substr(sprintf('%o', fileperms($path)), -4));
$folder->chmod(0775);
$this->assertEquals('0775', substr(sprintf('%o', fileperms($path)), -4));
$folder->delete();
}

/**
* test tree
*/
public function test_tree()
{
$path = TMP_TESTS . 'test' . DS . 'tree';
$folder = new BcFolder($path);
$folder->create();
$result = $folder->tree();
$this->assertEquals($path, $result[0][0]);
(new BcFile($path. DS . 'test.txt'))->create();
$result = $folder->tree();
$this->assertEquals($path, $result[0][0]);
$this->assertEquals($path. DS . 'test.txt', $result[1][0]);
$folder->delete();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace BaserCore\Test\TestCase\Utility;

use BaserCore\Utility\BcUpdateLog;
use Cake\Filesystem\File;

/**
* BcUpdateLogTest
Expand Down