Skip to content
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
4 changes: 4 additions & 0 deletions app/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -28,6 +30,8 @@ abstract class BaseController extends Controller
// protected $session;

/**
* @param CLIRequest|IncomingRequest $request
*
* @return void
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
Expand Down
2 changes: 2 additions & 0 deletions system/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Controller
/**
* Constructor.
*
* @param CLIRequest|IncomingRequest $request
*
* @return void
*
* @throws HTTPException|RedirectException
Expand Down
2 changes: 2 additions & 0 deletions system/RESTful/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ abstract class BaseResource extends Controller
/**
* Constructor.
*
* @param CLIRequest|IncomingRequest $request
*
* @return void
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
Expand Down
4 changes: 1 addition & 3 deletions system/Session/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class FileHandler extends BaseHandler
{
/**
* Where to save the session files to.
*
* @var string
*/
protected $savePath;

Expand Down Expand Up @@ -68,7 +66,7 @@ public function __construct(SessionConfig $config, string $ipAddress)
{
parent::__construct($config, $ipAddress);

if ($this->savePath !== '') {
if (is_string($this->savePath) && $this->savePath !== '') {
$this->savePath = rtrim($this->savePath, '/\\');
ini_set('session.save_path', $this->savePath);
} else {
Expand Down
3 changes: 1 addition & 2 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CodeIgniter\Config\Factories;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
Expand Down Expand Up @@ -44,7 +43,7 @@ final class ControllerTest extends CIUnitTestCase
/**
* Current request.
*/
private Request $request;
private IncomingRequest $request;

/**
* Current response.
Expand Down
23 changes: 18 additions & 5 deletions tests/system/Honeypot/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ protected function setUp(): void
$superglobals->setServer('REQUEST_METHOD', 'POST');
$superglobals->setPost($this->config->name, 'hey');

$this->request = service('request', null, false);
$this->response = service('response');
$this->request = service('request', null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;
}

public function testAttachHoneypot(): void
Expand Down Expand Up @@ -106,7 +109,10 @@ public function testAttachHoneypotAndContainerWithCSP(): void
$config = new App();
$config->CSPEnabled = true;
Factories::injectMock('config', 'App', $config);
$this->response = service('response', $config, false);

$response = service('response', $config, false);
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

$this->config = new HoneypotConfig();
$this->honeypot = new Honeypot($this->config);
Expand All @@ -125,7 +131,10 @@ public function testNotAttachHoneypotWithCSP(): void
$config = new App();
$config->CSPEnabled = true;
Factories::injectMock('config', 'App', $config);
$this->response = service('response', $config, false);

$response = service('response', $config, false);
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

$this->config = new HoneypotConfig();
$this->honeypot = new Honeypot($this->config);
Expand Down Expand Up @@ -189,7 +198,11 @@ public function testHoneypotFilterAfter(): void
$uri = 'admin/foo/bar';

$this->response->setBody('<form></form>');
$this->response = $filters->run($uri, 'after');

$response = $filters->run($uri, 'after');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

$this->assertStringContainsString($this->config->name, (string) $this->response->getBody());
}

Expand Down
130 changes: 66 additions & 64 deletions tests/system/Models/EventsModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
#[Group('DatabaseLive')]
final class EventsModelTest extends LiveModelTestCase
{
/**
* @var EventModel
*/
protected $model;

protected function setUp(): void
{
parent::setUp();
$this->createModel(EventModel::class);
}

private function model(): EventModel
{
$this->assertInstanceOf(EventModel::class, $this->model);

return $this->model;
}

public function testInsertEvent(): void
{
$data = [
Expand All @@ -43,9 +45,9 @@ public function testInsertEvent(): void
'deleted' => 0,
];

$this->model->insert($data);
$this->assertTrue($this->model->hasToken('beforeInsert'));
$this->assertTrue($this->model->hasToken('afterInsert'));
$this->model()->insert($data);
$this->assertTrue($this->model()->hasToken('beforeInsert'));
$this->assertTrue($this->model()->hasToken('afterInsert'));
}

public function testUpdateEvent(): void
Expand All @@ -57,106 +59,106 @@ public function testUpdateEvent(): void
'deleted' => 0,
];

$id = $this->model->insert($data);
$id = $this->model()->insert($data);

$this->model->update($id, $data);
$this->assertTrue($this->model->hasToken('beforeUpdate'));
$this->assertTrue($this->model->hasToken('afterUpdate'));
$this->model()->update($id, $data);
$this->assertTrue($this->model()->hasToken('beforeUpdate'));
$this->assertTrue($this->model()->hasToken('afterUpdate'));
}

public function testDeleteEvent(): void
{
$this->model->delete(1);
$this->assertTrue($this->model->hasToken('beforeDelete'));
$this->assertTrue($this->model->hasToken('afterDelete'));
$this->model()->delete(1);
$this->assertTrue($this->model()->hasToken('beforeDelete'));
$this->assertTrue($this->model()->hasToken('afterDelete'));
}

public function testFindEvent(): void
{
$this->model->find(1);
$this->assertTrue($this->model->hasToken('beforeFind'));
$this->assertTrue($this->model->hasToken('afterFind'));
$this->model()->find(1);
$this->assertTrue($this->model()->hasToken('beforeFind'));
$this->assertTrue($this->model()->hasToken('afterFind'));
}

public function testBeforeFindReturnsData(): void
{
$this->model->beforeFindReturnData = true;
$this->model()->beforeFindReturnData = true;

$result = $this->model->find(1);
$this->assertTrue($this->model->hasToken('beforeFind'));
$result = $this->model()->find(1);
$this->assertTrue($this->model()->hasToken('beforeFind'));
$this->assertSame('foobar', $result);
}

public function testBeforeFindReturnDataPreventsAfterFind(): void
{
$this->model->beforeFindReturnData = true;
$this->model->find(1);
$this->assertFalse($this->model->hasToken('afterFind'));
$this->model()->beforeFindReturnData = true;
$this->model()->find(1);
$this->assertFalse($this->model()->hasToken('afterFind'));
}

public function testFindEventSingletons(): void
{
// afterFind
$this->model->first();
$this->assertTrue($this->model->eventData['singleton']);
$this->model()->first();
$this->assertTrue($this->model()->eventData['singleton']);

$this->model->find(1);
$this->assertTrue($this->model->eventData['singleton']);
$this->model()->find(1);
$this->assertTrue($this->model()->eventData['singleton']);

$this->model->find();
$this->assertFalse($this->model->eventData['singleton']);
$this->model()->find();
$this->assertFalse($this->model()->eventData['singleton']);

$this->model->findAll();
$this->assertFalse($this->model->eventData['singleton']);
$this->model()->findAll();
$this->assertFalse($this->model()->eventData['singleton']);

// beforeFind
$this->model->beforeFindReturnData = true;
$this->model()->beforeFindReturnData = true;

$this->model->first();
$this->assertTrue($this->model->eventData['singleton']);
$this->model()->first();
$this->assertTrue($this->model()->eventData['singleton']);

$this->model->find(1);
$this->assertTrue($this->model->eventData['singleton']);
$this->model()->find(1);
$this->assertTrue($this->model()->eventData['singleton']);

$this->model->find();
$this->assertFalse($this->model->eventData['singleton']);
$this->model()->find();
$this->assertFalse($this->model()->eventData['singleton']);

$this->model->findAll();
$this->assertFalse($this->model->eventData['singleton']);
$this->model()->findAll();
$this->assertFalse($this->model()->eventData['singleton']);
}

public function testAllowCallbacksFalsePreventsTriggers(): void
{
$this->model->allowCallbacks(false)->find(1);
$this->assertFalse($this->model->hasToken('afterFind'));
$this->model()->allowCallbacks(false)->find(1);
$this->assertFalse($this->model()->hasToken('afterFind'));
}

public function testAllowCallbacksTrueFiresTriggers(): void
{
$this->setPrivateProperty($this->model, 'allowCallbacks', false);
$this->model->allowCallbacks(true)->find(1);
$this->assertTrue($this->model->hasToken('afterFind'));
$this->setPrivateProperty($this->model(), 'allowCallbacks', false);
$this->model()->allowCallbacks(true)->find(1);
$this->assertTrue($this->model()->hasToken('afterFind'));
}

public function testAllowCallbacksResetsAfterTrigger(): void
{
$this->model->allowCallbacks(false)->find(1);
$this->model->delete(1);
$this->model()->allowCallbacks(false)->find(1);
$this->model()->delete(1);

$this->assertFalse($this->model->hasToken('afterFind'));
$this->assertTrue($this->model->hasToken('afterDelete'));
$this->assertFalse($this->model()->hasToken('afterFind'));
$this->assertTrue($this->model()->hasToken('afterDelete'));
}

public function testAllowCallbacksUsesModelProperty(): void
{
$this->setPrivateProperty($this->model, 'allowCallbacks', false);
$this->setPrivateProperty($this->model, 'tempAllowCallbacks', false); // Was already set by the constructor
$this->setPrivateProperty($this->model(), 'allowCallbacks', false);
$this->setPrivateProperty($this->model(), 'tempAllowCallbacks', false); // Was already set by the constructor

$this->model->find(1);
$this->model->delete(1);
$this->model()->find(1);
$this->model()->delete(1);

$this->assertFalse($this->model->hasToken('afterFind'));
$this->assertFalse($this->model->hasToken('afterDelete'));
$this->assertFalse($this->model()->hasToken('afterFind'));
$this->assertFalse($this->model()->hasToken('afterDelete'));
}

public function testInvalidEventException(): void
Expand All @@ -168,11 +170,11 @@ public function testInvalidEventException(): void
'deleted' => 0,
];

$this->setPrivateProperty($this->model, 'beforeInsert', ['anotherBeforeInsertMethod']);
$this->setPrivateProperty($this->model(), 'beforeInsert', ['anotherBeforeInsertMethod']);

$this->expectException(DataException::class);
$this->expectExceptionMessage('"anotherBeforeInsertMethod" is not a valid Model Event callback.');
$this->model->insert($data);
$this->model()->insert($data);
}

public function testInsertBatchEvent(): void
Expand All @@ -192,9 +194,9 @@ public function testInsertBatchEvent(): void
],
];

$this->model->insertBatch($data);
$this->assertTrue($this->model->hasToken('beforeInsertBatch'));
$this->assertTrue($this->model->hasToken('afterInsertBatch'));
$this->model()->insertBatch($data);
$this->assertTrue($this->model()->hasToken('beforeInsertBatch'));
$this->assertTrue($this->model()->hasToken('afterInsertBatch'));
}

public function testUpdateBatchEvent(): void
Expand All @@ -210,8 +212,8 @@ public function testUpdateBatchEvent(): void
],
];

$this->model->updateBatch($data, 'name');
$this->assertTrue($this->model->hasToken('beforeUpdateBatch'));
$this->assertTrue($this->model->hasToken('afterUpdateBatch'));
$this->model()->updateBatch($data, 'name');
$this->assertTrue($this->model()->hasToken('beforeUpdateBatch'));
$this->assertTrue($this->model()->hasToken('afterUpdateBatch'));
}
}
Loading
Loading