Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Oct 13, 2023
2 parents 5210e5e + ce62326 commit 1e100f9
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 18 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 16,
'count' => 13,
'path' => __DIR__ . '/system/Database/Forge.php',
];
$ignoreErrors[] = [
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function run()
// Create CLIRequest
$appConfig = config(App::class);
Services::createRequest($appConfig, true);
// Load Routes
Services::routes()->loadRoutes();

$runner = Services::commands();
$params = array_merge(CLI::getSegments(), CLI::getOptions());
Expand Down
8 changes: 5 additions & 3 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private function configureKint(): void
* This is "the loop" if you will. The main entry point into the script
* that gets the required class instances, fires off the filters,
* tries to route the response, loads the controller and generally
* makes all of the pieces work together.
* makes all the pieces work together.
*
* @return ResponseInterface|void
*/
Expand Down Expand Up @@ -694,6 +694,8 @@ protected function forceSecureAccess($duration = 31_536_000)
* @return false|ResponseInterface
*
* @throws Exception
*
* @deprecated 4.4.2 The parameter $config is deprecated. No longer used.
*/
public function displayCache(Cache $config)
{
Expand All @@ -713,7 +715,7 @@ public function displayCache(Cache $config)
/**
* Tells the app that the final output should be cached.
*
* @deprecated 4.4.0 Moved to ResponseCache::setTtl(). to No longer used.
* @deprecated 4.4.0 Moved to ResponseCache::setTtl(). No longer used.
*
* @return void
*/
Expand Down Expand Up @@ -789,7 +791,7 @@ public function displayPerformanceMetrics(string $output): string
* match a route against the current URI. If the route is a
* "redirect route", will also handle the redirect.
*
* @param RouteCollectionInterface|null $routes An collection interface to use in place
* @param RouteCollectionInterface|null $routes A collection interface to use in place
* of the config file.
*
* @return string|string[]|null Route filters, that is, the filters specified in the routes file
Expand Down
14 changes: 6 additions & 8 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ public function processIndexes(string $table): bool
$sqls = [];
$fk = $this->foreignKeys;

if (empty($this->fields)) {
if ($this->fields === []) {
$this->fields = array_flip(array_map(
static fn ($columnName) => $columnName->name,
$this->db->getFieldData($this->db->DBPrefix . $table)
Expand All @@ -1082,20 +1082,18 @@ public function processIndexes(string $table): bool

$fields = $this->fields;

if (! empty($this->keys)) {
if ($this->keys !== []) {
$sqls = $this->_processIndexes($this->db->DBPrefix . $table, true);
}

$pk = $this->_processPrimaryKeys($table, true);

if ($pk !== '') {
$sqls[] = $pk;
}
if ($this->primaryKeys !== []) {
$sqls[] = $this->_processPrimaryKeys($table, true);
}

$this->foreignKeys = $fk;
$this->fields = $fields;

if (! empty($this->foreignKeys)) {
if ($this->foreignKeys !== []) {
$sqls = array_merge($sqls, $this->_processForeignKeys($table, true));
}

Expand Down
12 changes: 6 additions & 6 deletions system/Honeypot/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ public function attachHoneypot(ResponseInterface $response)

$prepField = $this->prepareTemplate($this->config->template);

$body = $response->getBody();
$body = str_ireplace('</form>', $prepField . '</form>', $body);
$bodyBefore = $response->getBody();
$bodyAfter = str_ireplace('</form>', $prepField . '</form>', $bodyBefore);

if ($response->getCSP()->enabled()) {
if ($response->getCSP()->enabled() && ($bodyBefore !== $bodyAfter)) {
// Add style tag for the container tag in the head tag.
$style = '<style ' . csp_style_nonce() . '>#' . $this->config->containerId . ' { display:none }</style>';
$body = str_ireplace('</head>', $style . '</head>', $body);
$style = '<style ' . csp_style_nonce() . '>#' . $this->config->containerId . ' { display:none }</style>';
$bodyAfter = str_ireplace('</head>', $style . '</head>', $bodyAfter);
}

$response->setBody($body);
$response->setBody($bodyAfter);
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,87 @@ public function testProcessIndexes(): void
$this->forge->dropTable('user2', true);
}

public function testProcessIndexesWithKeyOnly(): void
{
// make sure tables don't exist
$this->forge->dropTable('actions', true);

$this->createActionsTable();
$this->forge->addKey('name', false, false, 'db_actions_name');

// create indexes
$this->forge->processIndexes('actions');

// get a list of all indexes
$allIndexes = $this->db->getIndexData('actions');

// check that db_actions_name key exists
$indexes = array_filter(
$allIndexes,
static fn ($index) => ($index->name === 'db_actions_name')
&& ($index->fields === [0 => 'name'])
);
$this->assertCount(1, $indexes);

// drop tables to avoid any future conflicts
$this->forge->dropTable('actions', true);
}

public function testProcessIndexesWithPrimaryKeyOnly(): void
{
// make sure tables don't exist
$this->forge->dropTable('actions', true);

$this->createActionsTable();
$this->forge->addPrimaryKey('id');

// create indexes
$this->forge->processIndexes('actions');

// get a list of all indexes
$allIndexes = $this->db->getIndexData('actions');

// check that the primary key exists
$indexes = array_filter(
$allIndexes,
static fn ($index) => $index->type === 'PRIMARY'
);
$this->assertCount(1, $indexes);

// drop tables to avoid any future conflicts
$this->forge->dropTable('actions', true);
}

public function testProcessIndexesWithForeignKeyOnly(): void

Check warning on line 1711 in tests/system/Database/Live/ForgeTest.php

View workflow job for this annotation

GitHub Actions / database-live-tests (8.1, SQLSRV, 5.7) / Database Live Tests

Took 0.70s from 0.50s limit to run CodeIgniter\\Database\\Live\\ForgeTest::testProcessIndexesWithForeignKeyOnly
{
// make sure tables don't exist
$this->forge->dropTable('actions', true);
$this->forge->dropTable('user2', true);

$this->createUser2TableWithKeys();
$this->populateUser2Table();
$this->createActionsTable();

// SQLite does not support custom foreign key name
if ($this->db->DBDriver === 'SQLite3') {
$this->forge->addForeignKey('userid', 'user', 'id');
$this->forge->addForeignKey('userid2', 'user2', 'id');
} else {
$this->forge->addForeignKey('userid', 'user', 'id', '', '', 'db_actions_userid_foreign');
$this->forge->addForeignKey('userid2', 'user2', 'id', '', '', 'db_actions_userid2_foreign');
}

// create indexes
$this->forge->processIndexes('actions');

// check that the two foreign keys exist
$this->assertCount(2, $this->db->getForeignKeyData('actions'));

// drop tables to avoid any future conflicts
$this->forge->dropTable('actions', true);
$this->forge->dropTable('user2', true);
}

private function createUser2TableWithKeys(): void
{
$fields = [
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Honeypot/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ public function testAttachHoneypotAndContainerWithCSP(): void
$this->assertMatchesRegularExpression($regex, $this->response->getBody());
}

public function testNotAttachHoneypotWithCSP(): void
{
$this->resetServices();

$config = new App();
$config->CSPEnabled = true;
Factories::injectMock('config', 'App', $config);
$this->response = Services::response($config, false);

$this->config = new HoneypotConfig();
$this->honeypot = new Honeypot($this->config);

$this->response->setBody('<head></head><body></body>');
$this->honeypot->attachHoneypot($this->response);

$this->assertSame('<head></head><body></body>', $this->response->getBody());
}

public function testHasntContent(): void
{
unset($_POST[$this->config->name]);
Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/changelogs/v4.4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Deprecations
details.
- **CLI:** The public property ``CLI::$readline_support`` and ``CLI::$wait_msg``
are deprecated. These methods will be protected.
- **CodeIgniter:** The parameter ``$config`` for the ``displayCache()`` method is
deprecated. It was not used.

Bugs Fixed
**********
Expand All @@ -42,6 +44,8 @@ Bugs Fixed
Page Not Found.
- **Spark:** Fixed a bug that caused spark to not display exceptions in the
production mode or to display backtrace in json when an exception occurred.
- **Forge:** Fixed a bug where adding a Primary Key to an existing table was
ignored if there were no other Keys added too.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down

0 comments on commit 1e100f9

Please sign in to comment.