66 lines
1.8 KiB
PHP
66 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace CakeProducts\Test\TestCase\Controller;
|
|
|
|
use Cake\Core\Configure;
|
|
use Cake\TestSuite\IntegrationTestTrait;
|
|
use Cake\TestSuite\TestCase;
|
|
use FilesystemIterator;
|
|
use RecursiveDirectoryIterator;
|
|
use RecursiveIteratorIterator;
|
|
|
|
/**
|
|
* BaseControllerTest
|
|
*
|
|
* Used to make logging in easier and to handle folder structure for product images
|
|
*/
|
|
class BaseControllerTest extends TestCase {
|
|
|
|
use IntegrationTestTrait;
|
|
|
|
public function loginUserByRole(string $role = 'admin'): void {
|
|
$this->session(['Auth.User.id' => 1]);
|
|
$this->session(['Auth.id' => 1]);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testTest() {
|
|
$this->assertEquals(1, 1);
|
|
}
|
|
|
|
/**
|
|
* setUp method
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
$toCopy = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'images' . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png';
|
|
$productsFolder = PLUGIN_ROOT . DS . 'tests' . DS . 'test_app' . DS . 'webroot' . DS . 'uploads' . DS
|
|
. 'images' . DS . 'products' . DS . 'cfc98a9a-29b2-44c8-b587-8156adc05317';
|
|
$newName = $productsFolder . DS . '2c386086-f4c5-4093-bea5-ee9c29479f58.png';
|
|
if (file_exists($toCopy)) {
|
|
if (!file_exists($productsFolder)) {
|
|
mkdir($productsFolder, 0775, true);
|
|
}
|
|
copy($toCopy, $newName);
|
|
}
|
|
}
|
|
|
|
protected function tearDown(): void {
|
|
parent::tearDown(); // TODO: Change the autogenerated stub
|
|
|
|
$path = Configure::readOrFail('CakeProducts.photos.directory');
|
|
if (file_exists($path)) {
|
|
$di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
|
|
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
|
|
foreach ($ri as $file) {
|
|
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|