testing brought in

This commit is contained in:
2026-03-17 01:35:40 -07:00
parent 39334d8e4a
commit 4f996eb7d2
20 changed files with 641 additions and 192 deletions

View File

@@ -13,9 +13,9 @@ use Cake\Routing\RouteBuilder;
/**
* Plugin for CheeseCake
*/
class CheeseCakePlugin extends BasePlugin
{
/**
class CheeseCakePlugin extends BasePlugin {
/**
* Load all the plugin configuration and bootstrap logic.
*
* The host application is provided as an argument. This allows you to load
@@ -24,11 +24,10 @@ class CheeseCakePlugin extends BasePlugin
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
}
public function bootstrap(PluginApplicationInterface $app): void {
}
/**
/**
* Add routes for the plugin.
*
* If your plugin has many routes and you would like to isolate them into a separate file,
@@ -37,48 +36,45 @@ class CheeseCakePlugin extends BasePlugin
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return void
*/
public function routes(RouteBuilder $routes): void
{
parent::routes($routes);
}
public function routes(RouteBuilder $routes): void {
parent::routes($routes);
}
/**
/**
* Add middleware for the plugin.
*
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
// Add your middlewares here
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue {
// Add your middlewares here
return $middlewareQueue;
}
return $middlewareQueue;
}
/**
/**
* Add commands for the plugin.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update.
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands): CommandCollection
{
// Add your commands here
public function console(CommandCollection $commands): CommandCollection {
// Add your commands here
$commands = parent::console($commands);
$commands = parent::console($commands);
return $commands;
}
return $commands;
}
/**
/**
* Register application container services.
*
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
* @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
*/
public function services(ContainerInterface $container): void
{
// Add your services here
}
public function services(ContainerInterface $container): void {
// Add your services here
}
}

View File

@@ -5,6 +5,5 @@ namespace CheeseCake\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
class AppController extends BaseController {
}

View File

@@ -6,65 +6,66 @@ use Cake\Core\Configure;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
trait OverrideTableTrait
{
/**
* @var Table|null
*/
protected ?Table $_table = null;
trait OverrideTableTrait {
/**
/**
* @var \Cake\ORM\Table|null
*/
protected ?Table $_table = null;
/**
* This object's default table alias.
*
* @var string|null
*/
protected ?string $defaultTable = null;
protected ?string $defaultTable = null;
/**
/**
* @var string
*/
protected string $_tableConfigKey = '';
protected string $_tableConfigKey = '';
/**
/**
* Gets the table instance
*
* @return Table
* @return \Cake\ORM\Table
*/
public function getTable(string|null $tableName)
{
if ($this->_table instanceof Table) {
return $this->_table;
}
$this->getTableConfigKey();
$table = $tableName;
if (!isset($table)) {
$table = $this->defaultTable;
if (Configure::read($this->_tableConfigKey)) {
$table = Configure::read($this->_tableConfigKey);
}
}
$this->_table = TableRegistry::getTableLocator()->get($table);
public function getTable(string|null $tableName) {
if ($this->_table instanceof Table) {
return $this->_table;
}
$this->getTableConfigKey();
$table = $tableName;
if (!isset($table)) {
$table = $this->defaultTable;
if (Configure::read($this->_tableConfigKey)) {
$table = Configure::read($this->_tableConfigKey);
}
}
$this->_table = TableRegistry::getTableLocator()->get($table);
return $this->_table;
}
return $this->_table;
}
protected function getTableConfigKey()
{
if (!$this->_tableConfigKey) {
$this->_tableConfigKey = $this->getPlugin() . '.' . $this->defaultTable . '.table';
}
/**
* @return string
*/
protected function getTableConfigKey() {
if (!$this->_tableConfigKey) {
$this->_tableConfigKey = $this->getPlugin() . '.' . $this->defaultTable . '.table';
}
return $this->_tableConfigKey;
}
return $this->_tableConfigKey;
}
/**
/**
* Set the users table
*
* @param Table $table table
* @param \Cake\ORM\Table $table table
* @return void
*/
public function setTable(Table $table)
{
$this->_table = $table;
}
}
public function setTable(Table $table) {
$this->_table = $table;
}
}

View File

@@ -8,9 +8,9 @@ use Cake\ORM\Entity;
/**
* Secure Entity - must explicitly pass fields that are acceptable to be updated on each newEntity/patchEntity call
*/
class SecureEntity extends Entity
{
/**
class SecureEntity extends Entity {
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
@@ -19,5 +19,6 @@ class SecureEntity extends Entity
*
* @var array<string, bool>
*/
protected array $_accessible = [];
protected array $_accessible = [];
}

View File

@@ -3,120 +3,163 @@ declare(strict_types=1);
namespace CheeseCake\View\Helper;
use Cake\Log\Log;
use Cake\Routing\Router;
use Cake\View\Helper;
use Cake\View\View;
/**
* ActiveLink helper
*/
class ActiveLinkHelper extends Helper
{
/**
class ActiveLinkHelper extends Helper {
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'activeClass' => 'active',
];
/**
protected array $_defaultConfig = [
'activeClass' => 'active',
];
/**
* List of helpers used by this helper
*
* @var string[]
*/
protected array $helpers = [
'Html',
'Url',
];
protected array $helpers = [
'Html',
'Url',
];
/**
/**
* @param array|string $title
* @param array|string|null $url
* @param array $options
*
* @return string
*/
public function link(array|string $title, array|string|null $url = null, array $options = []): string
{
$currentUrl = Router::parseRequest($this->getView()->getRequest());
if (!array_key_exists('target', $options) || !$currentUrl) {
return $this->Html->link($title, $url, $options);
}
$target = $options['target'];
public function link(array|string $title, array|string|null $url = null, array $options = []): string {
$currentUrl = $options['current'] ?? Router::parseRequest($this->getView()->getRequest());
if (!array_key_exists('target', $options) || !$currentUrl) {
return $this->Html->link($title, $url, $options);
}
$target = $options['target'];
$activeClass = $options['activeClass'] ?? $this->getConfig('activeClass');
unset($options['target']);
if (is_string($target)) {
return $this->_linkFromStringTarget($currentUrl, $target, $title, $url, $options);
}
if (!is_array($target)) {
return $this->Html->link($title, $url, $options);
}
unset($options['activeClass']);
if (is_string($target)) {
return $this->_linkFromStringTarget($currentUrl, $target, $title, $url, $activeClass, $options);
}
if (!is_array($target)) {
return $this->Html->link($title, $url, $options);
}
if (!array_key_exists('plugin', $currentUrl)) {
$currentUrl['plugin'] = false;
}
if (!array_key_exists('prefix', $currentUrl)) {
$currentUrl['prefix'] = false;
}
if (isset($target['or']) && $target['or']) {
foreach ($target['or'] as $singleTargetToMatch) {
if ($this->_matchesUrlFromArrayTarget($currentUrl, $singleTargetToMatch)) {
$options['class'] = $this->_addClass($options);
if (!array_key_exists('plugin', $currentUrl)) {
$currentUrl['plugin'] = false;
}
if (!array_key_exists('prefix', $currentUrl)) {
$currentUrl['prefix'] = false;
}
if (isset($target['or']) && $target['or']) {
foreach ($target['or'] as $singleTargetToMatch) {
if ($this->_matchesUrlFromArrayTarget($currentUrl, $singleTargetToMatch)) {
$options['class'] = $this->_addClass($options, $activeClass);
return $this->Html->link($title, $url, $options);
}
}
return $this->Html->link($title, $url, $options);
}
return $this->Html->link($title, $url, $options);
}
}
if (!$this->_matchesUrlFromArrayTarget($currentUrl, $target)) {
return $this->Html->link($title, $url, $options);
}
return $this->Html->link($title, $url, $options);
}
$options['class'] = $this->_addClass($options);
if (!$this->doesUrlMatchTarget($target, $currentUrl)) {
return $this->Html->link($title, $url, $options);
}
return $this->Html->link($title, $url, $options);
}
$options['class'] = $this->_addClass($options, $activeClass);
return $this->Html->link($title, $url, $options);
}
/**
/**
* @param array|string $targetUrl
* @param array|null $current |null current url
*
* @return bool
*/
public function doesUrlMatchTarget(array|string $targetUrl, array|null $current = null) {
if (!isset($current)) {
$current = Router::parseRequest($this->getView()->getRequest());
}
if (is_string($targetUrl) && Router::normalize($current) == Router::normalize($targetUrl)) {
return true;
}
if (is_array($targetUrl)) {
if (isset($targetUrl['or']) && $targetUrl['or']) {
foreach ($targetUrl['or'] as $singleTargetToMatch) {
$matched = $this->_matchesUrlFromArrayTarget($current, $singleTargetToMatch);
if ($matched) {
return true;
}
}
}
return $this->_matchesUrlFromArrayTarget($current, $targetUrl);
}
return false;
}
/**
* @param array $providedOptions
*
* @return string
*/
protected function _addClass(array $providedOptions): string
{
$activeClass = array_key_exists('activeClass', $providedOptions) ? $providedOptions['activeClass'] : $this->getConfig('activeClass');
return array_key_exists('class', $providedOptions) ? $providedOptions['class'] . ' ' . $activeClass : $activeClass;
}
protected function _addClass(array $providedOptions, string $toAdd): string {
return array_key_exists('class', $providedOptions) ? $providedOptions['class'] . ' ' . $toAdd : $toAdd;
}
/**
* @param array $current
* @param string $targetString
* @param string $title
* @param array|string|null $url
* @param array $options
*
* @return string
*/
protected function _linkFromStringTarget(array $current, string $targetString, string $title, array|string|null $url, string $activeClass, array $options) {
if (Router::normalize($current) == Router::normalize($targetString)) {
$options['class'] = $this->_addClass($options, $activeClass);
protected function _linkFromStringTarget(array $current, string $targetString, string $title, array|string|null $url, array $options)
{
if (Router::normalize($current) == Router::normalize($targetString)) {
$options['class'] = $this->_addClass($options);
return $this->Html->link($title, $url, $options);
}
return $this->Html->link($title, $url, $options);
}
return $this->Html->link($title, $url, $options);
}
/**
* @param array $current
* @param array $targetUrl
*
* @return bool
*/
protected function _matchesUrlFromArrayTarget(array $current, array $targetUrl) {
foreach ($targetUrl as $targetKey => $targetValue) {
if (is_array($targetValue)) {
if (!in_array($current[$targetKey], $targetValue)) {
return false;
}
return $this->Html->link($title, $url, $options);
}
continue;
}
if (!array_key_exists($targetKey, $current) || $targetValue != $current[$targetKey]) {
return false;
}
}
protected function _matchesUrlFromArrayTarget(array $current, array $targetUrl)
{
foreach ($targetUrl as $targetKey => $targetValue) {
if (is_array($targetValue)) {
if (!in_array($current[$targetKey], $targetValue)) {
return false;
}
return true;
}
continue;
}
if (!array_key_exists($targetKey, $current) || $targetValue != $current[$targetKey]) {
return false;
}
}
return true;
}
}