diff --git a/config/playground.php b/config/playground.php index 5ed7ef4..5545aa4 100644 --- a/config/playground.php +++ b/config/playground.php @@ -1,9 +1,9 @@ env('PLAYGROUND_USER', 'App\\Models\\User'), - 'user_id' => env('PLAYGROUND_USER_ID', 'uuid'), - 'user_table' => env('PLAYGROUND_USER_TABLE', 'users'), + // 'user' => env('PLAYGROUND_USER', 'App\\Models\\User'), + // 'user_id' => env('PLAYGROUND_USER_ID', 'uuid'), + // 'user_table' => env('PLAYGROUND_USER_TABLE', 'users'), 'packages' => is_string(env('PLAYGROUND_PACKAGES', 'playground')) ? array_map('trim', explode(',', env('PLAYGROUND_PACKAGES', 'playground'))) : [], // 'auth' => [ // /** @@ -28,13 +28,13 @@ 'date' => [ 'sql' => env('PLAYGROUND_DATE_SQL', 'Y-m-d H:i:s'), ], - 'purifier' => [ - 'iframes' => env( - 'PLAYGROUND_PURIFIER_IFRAMES', - '%^(https?:)?(\/\/www\.youtube(?:-nocookie)?\.com\/embed\/|\/\/player\.vimeo\.com\/)%' - ), - 'path' => env('PLAYGROUND_PURIFIER_PATH', ''), - ], + // 'purifier' => [ + // 'iframes' => env( + // 'PLAYGROUND_PURIFIER_IFRAMES', + // '%^(https?:)?(\/\/www\.youtube(?:-nocookie)?\.com\/embed\/|\/\/player\.vimeo\.com\/)%' + // ), + // 'path' => env('PLAYGROUND_PURIFIER_PATH', ''), + // ], /* |-------------------------------------------------------------------------- @@ -43,9 +43,9 @@ | */ - 'testing' => [ - 'password' => env('PLAYGROUND_TESTING_PASSWORD'), - 'hashed' => (bool) env('PLAYGROUND_TESTING_HASHED', false), - ], + // 'testing' => [ + // 'password' => env('PLAYGROUND_TESTING_PASSWORD'), + // 'hashed' => (bool) env('PLAYGROUND_TESTING_HASHED', false), + // ], ]; diff --git a/src/Filters/ContentTrait.php b/src/Filters/ContentTrait.php deleted file mode 100644 index 1891f48..0000000 --- a/src/Filters/ContentTrait.php +++ /dev/null @@ -1,119 +0,0 @@ -getHtmlPurifier()->purify($item); - } - - /** - * Exorcise all html from the string. - * - * @param string $item The string to purify - * @return string - */ - public static function exorcise($item) - { - return htmlspecialchars( - strip_tags($item), - ENT_HTML5 - ); - } - - /** - * Get HTMLPurifier - * - * @param array $config - */ - public function getHtmlPurifier(array $config = []): HTMLPurifier - { - if ($this->purifier === null) { - $config = empty($config) ? config('playground.purifier') : $config; - - $hpc = \HTMLPurifier_Config::createDefault(); - - if (is_array($config) - && ! empty($config['iframes']) - && is_string($config['iframes']) - ) { - $hpc->set('HTML.SafeIframe', true); - $hpc->set('URI.SafeIframeRegexp', $config['iframes']); - } - - if (is_array($config) - && ! empty($config['path']) - && is_string($config['path']) - ) { - $hpc->set('Cache.SerializerPath', $config['path']); - } - - $this->setHtmlPurifier(new HTMLPurifier($hpc)); - } - - return $this->purifier; - } - - /** - * Set HTMLPurifier - * - * @param HTMLPurifier $purifier The HTMLPurifier instance - */ - public function setHtmlPurifier(HTMLPurifier $purifier): self - { - if ($this->purifier === null) { - $this->purifier = $purifier; - } - - return $this; - } - - //########################################################################### - // - // URI handling - // - //########################################################################### - - /** - * encodeURIComponent - * - * This method is supposed to be identical to the function in JavaScript. - * - * @link http://stackoverflow.com/questions/1734250/what-is-the-equivalent-of-javascripts-encodeuricomponent-in-php - * - * @param string $str The string to encode. - * @return string Returns an encoded URL for embedding - */ - public static function encodeURIComponent(string $str): string - { - return strtr(rawurlencode($str), [ - '%21' => '!', - '%2A' => '*', - '%27' => "'", - '%28' => '(', - '%29' => ')', - ]); - } -} diff --git a/src/Filters/ModelTrait.php b/src/Filters/ModelTrait.php deleted file mode 100644 index c0b61b4..0000000 --- a/src/Filters/ModelTrait.php +++ /dev/null @@ -1,325 +0,0 @@ - Returns an array. - */ - public function filterArray(mixed $value): array - { - if (is_array($value)) { - return $value; - } elseif (! empty($value) && is_string($value)) { - return (array) $value; - } - - return []; - } - - /** - * Filter an array and encode it to json. - * - * NOTE: This may not be necessary if the field has been cast in the model. - * - * @param mixed $value The value to filter. - * @return string|false Returns an array converted to JSON. - */ - public function filterArrayToJson(mixed $value): string|false - { - if (is_array($value)) { - return json_encode($value); - } elseif (is_string($value)) { - return $value; - } else { - return json_encode([]); - } - } - - /** - * Filter a bit value - * - * @param int $value The value to filter. - * @param int $exponent The maximum power of the exponent to sum. - */ - public function filterBits($value, $exponent = 0): int - { - $exponent = intval(abs($exponent)); - - /** - * @var int $pBits The summed bit power values. - */ - $pBits = 0; - // $pBits = 4 + 2 + 1; - - for ($i = 0; $i <= $exponent; $i++) { - $pBits += pow(2, $i); - } - - return intval(abs($value)) & $pBits; - } - - /** - * Filter a boolean value - * - * @param mixed $value The value to filter. - */ - public function filterBoolean(mixed $value): bool - { - if (is_string($value) && ! is_numeric($value)) { - return strtolower($value) === 'true'; - } elseif (is_numeric($value)) { - return $value > 0; - } else { - return (bool) $value; - } - } - - /** - * Filter a date value as an SQL UTC string. - * - * @param string $value The date to filter. - * @param string $locale i18n - */ - public function filterDate(mixed $value, $locale = 'en-US'): ?string - { - if (empty($value) || ! ( - is_string($value) - || $value instanceof \DateTimeInterface - )) { - return null; - } - - $PLAYGROUND_DATE_SQL = config('playground.date.sql'); - $PLAYGROUND_DATE_SQL = empty($PLAYGROUND_DATE_SQL) || ! is_string($PLAYGROUND_DATE_SQL) ? 'Y-m-d H:i:s' : $PLAYGROUND_DATE_SQL; - - return Carbon::parse($value)->format($PLAYGROUND_DATE_SQL); - // return Carbon::parse($value)->format(config('playground.date.sql', 'Y-m-d H:i:s')); - } - - /** - * Filter a date value as a Carbon date. - * - * @param string $value The date to filter. - * @param string $locale i18n - */ - public function filterDateAsCarbon($value, $locale = 'en-US'): ?Carbon - { - if (empty($value)) { - return null; - } - - return new Carbon($value); - } - - /** - * Filter an email address. - * - * @param mixed $email The address to filter. - */ - public function filterEmail(mixed $email): string - { - $email = is_string($email) ? filter_var($email, FILTER_SANITIZE_EMAIL) : ''; - - return is_string($email) ? $email : ''; - } - - /** - * Filter a float value - * - * @param mixed $value The value to filter. - * @param string $locale i18n - */ - public function filterFloat(mixed $value, $locale = 'en-US'): ?float - { - if ($value === '' || $value === null) { - return null; - } - - return is_numeric($value) ? floatval($value) : null; - // return (new \NumberFormatter( - // $locale, - // \NumberFormatter::DECIMAL - // ))->parse($value); - } - - /** - * Filter HTML from content. - * - * FILTER_FLAG_NO_ENCODE_QUOTES - do not encode quotes. - * - * @param string $content The string to filter. - */ - public function filterHtml(string $content): string - { - $content = filter_var( - $content, - FILTER_SANITIZE_STRING, - FILTER_FLAG_NO_ENCODE_QUOTES - ); - - return is_string($content) ? $content : ''; - } - - /** - * Filter an integer value - * - * @param mixed $value The value to filter. - * @param string $locale i18n - */ - public function filterInteger(mixed $value, $locale = 'en-US'): int - { - if ($value === '' || $value === null) { - return 0; - } - - // $value = (new \NumberFormatter( - // $locale, - // \NumberFormatter::DECIMAL - // ))->parse($value, \NumberFormatter::TYPE_INT64); - - return is_numeric($value) ? intval($value) : 0; - // return is_int($value) ? $value : 0; - } - - /** - * Filter an integer value ID. - * - * @param mixed $value The value to filter. - */ - public function filterIntegerId(mixed $value): ?int - { - return is_numeric($value) && ($value > 0) ? (int) $value : null; - } - - /** - * Filter a positive integer value or return zero. - * - * @param mixed $value The value to filter. - * @param bool $absolute Use `abs()` on the value to convert negative to positive. - */ - public function filterIntegerPositive(mixed $value, $absolute = true): int - { - $value = is_scalar($value) ? intval($value) : 0; - - return $absolute && ($value < 0) ? (int) abs($value) : $value; - } - - /** - * Filter a percent value - * - * NOTE: Only removes the percent sign. - * - * @param mixed $value The value to filter. - * @param string $locale i18n - */ - public function filterPercent(mixed $value, $locale = 'en-US'): ?float - { - if ($value === '' || $value === null) { - return null; - } - - if (is_string($value)) { - $value = str_replace('%', '', $value); - } - - return $this->filterFloat($value, $locale); - } - - /** - * Filter the status - * - * @param array $input The status input. - * @return array - */ - public function filterStatus(array $input = []): array - { - if (! isset($input['status'])) { - return $input; - } - - if (is_numeric($input['status'])) { - $input['status'] = (int) abs($input['status']); - - return $input; - } - - if (is_array($input['status'])) { - foreach ($input['status'] as $key => $value) { - $input['status'][$key] = (bool) $value; - } - } - - return $input; - } - - /** - * Filter system fields - * - * @param array $input The system fields input. - * @return array - */ - public function filterSystemFields(array $input = []): array - { - // Filter system fields. - if (isset($input['gids']) && is_numeric($input['gids'])) { - $input['gids'] = (int) abs($input['gids']); - } - - /** - * @var int $pBits The allowed permission bits: rwx - */ - $pBits = 4 + 2 + 1; - - if (isset($input['po']) && is_numeric($input['po'])) { - $input['po'] = intval(abs($input['po'])) & $pBits; - } - - if (isset($input['pg']) && is_numeric($input['pg'])) { - $input['pg'] = intval(abs($input['pg'])) & $pBits; - } - - if (isset($input['pw']) && is_numeric($input['pw'])) { - $input['pw'] = intval(abs($input['pw'])) & $pBits; - } - - if (isset($input['rank']) && is_numeric($input['rank'])) { - $input['rank'] = (int) $input['rank']; - } - - if (isset($input['size']) && is_numeric($input['size'])) { - $input['size'] = (int) $input['size']; - } - - return $input; - } - - /** - * Filter a UUID - * - * @param mixed $value The value to filter. - */ - public function filterUuid(mixed $value): ?string - { - return is_string($value) && Uuid::isValid($value) - ? $value : null; - } -} diff --git a/src/Models/Traits/WithCreator.php b/src/Models/Traits/WithCreator.php index 622a08b..a4a1647 100644 --- a/src/Models/Traits/WithCreator.php +++ b/src/Models/Traits/WithCreator.php @@ -19,7 +19,7 @@ public function creator(): HasOne /** * @var class-string $userClass */ - $userClass = config('playground.user', '\\App\\Models\\User'); + $userClass = config('auth.providers.users.model', '\\App\\Models\\User'); return $this->hasOne($userClass, 'id', 'created_by_id'); } diff --git a/src/Models/Traits/WithModifier.php b/src/Models/Traits/WithModifier.php index 2bedb73..beb8e38 100644 --- a/src/Models/Traits/WithModifier.php +++ b/src/Models/Traits/WithModifier.php @@ -19,7 +19,7 @@ public function modifier(): HasOne /** * @var class-string $userClass */ - $userClass = config('playground.user', '\\App\\Models\\User'); + $userClass = config('auth.providers.users.model', '\\App\\Models\\User'); return $this->hasOne($userClass, 'id', 'modified_by_id'); } diff --git a/src/Models/Traits/WithOwner.php b/src/Models/Traits/WithOwner.php index fda65e8..302ad9b 100644 --- a/src/Models/Traits/WithOwner.php +++ b/src/Models/Traits/WithOwner.php @@ -19,7 +19,7 @@ public function owner(): HasOne /** * @var class-string $userClass */ - $userClass = config('playground.user', '\\App\\Models\\User'); + $userClass = config('auth.providers.users.model', '\\App\\Models\\User'); return $this->hasOne($userClass, 'id', 'owned_by_id'); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7870e04..af46d6d 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -24,10 +24,10 @@ public function boot(): void $config = config($this->package); if (! empty($config['load']) && is_array($config['load'])) { - $this->loadTranslationsFrom( - dirname(__DIR__).'/lang', - 'playground' - ); + // $this->loadTranslationsFrom( + // dirname(__DIR__).'/lang', + // 'playground' + // ); if ($this->app->runningInConsole()) { // Publish configuration @@ -36,8 +36,9 @@ public function boot(): void ], 'playground-config'); } - $this->about($config); } + + $this->about($config); } /** @@ -45,22 +46,18 @@ public function boot(): void */ public function about(array $config): void { - $purifier = ! empty($config['purifier']) && is_array($config['purifier']) ? $config['purifier'] : []; $packages = ! empty($config['packages']) && is_array($config['packages']) ? $config['packages'] : []; $version = $this->version(); + /** + * @var class-string $auth_providers_users_model + */ $auth_providers_users_model = config('auth.providers.users.model'); AboutCommand::add('Playground', fn () => [ - 'Purifier [path]' => sprintf('[%s]', empty($purifier['path']) ? 'null' : $purifier['path']), - 'Purifier [iframes]' => sprintf('[%s]', is_string($purifier['iframes']) ? $purifier['iframes'] : ''), - 'User [auth.providers.users.model]' => sprintf('[%s]', is_string($auth_providers_users_model) ? $auth_providers_users_model : ''), - 'User [playground.user]' => sprintf('[%s]', is_string($config['user']) ? $config['user'] : ''), - - 'User Model ID Type' => $config['user_id'], - 'User Model Table' => $config['user_table'], + 'User Primary' => $this->userPrimaryKeyType($auth_providers_users_model), 'Packages' => implode(', ', $packages), 'Package' => $this->package, @@ -68,6 +65,40 @@ public function about(array $config): void ]); } + /** + * @param ?class-string $auth_providers_users_model + */ + public function userPrimaryKeyType(string $auth_providers_users_model = null): string + { + $model_info = ''; + $user = null; + if (! $auth_providers_users_model || ! class_exists($auth_providers_users_model)) { + return 'invalid'; + } + + try { + /** + * @var \Illuminate\Contracts\Auth\Authenticatable + */ + $user = new $auth_providers_users_model; + // dump($user->toArray()); + + if (in_array(\Illuminate\Database\Eloquent\Concerns\HasUuids::class, class_uses_recursive($user)) + && ! $user->getIncrementing() + ) { + $model_info = 'UUID'; + } elseif ($user->getIncrementing()) { + $model_info = 'increments'; + } + + return $model_info; + } catch (\Throwable $th) { + \Log::debug($th->__toString()); + + return 'error'; + } + } + public function register(): void { $this->mergeConfigFrom( diff --git a/tests/Feature/Models/User/ModelTest.php b/tests/Feature/Models/User/ModelTest.php index 88df414..3c803cb 100644 --- a/tests/Feature/Models/User/ModelTest.php +++ b/tests/Feature/Models/User/ModelTest.php @@ -34,9 +34,6 @@ public function test_save_model_in_database(): void $attributes = $user->toArray(); $this->assertIsArray($attributes); - dump([ - '$attributes' => $attributes, - ]); $this->assertArrayHasKey('abilities', $attributes); $this->assertSame(['site:*'], $attributes['abilities']); diff --git a/tests/Feature/TestCase.php b/tests/Feature/TestCase.php index 9701079..6b1d096 100644 --- a/tests/Feature/TestCase.php +++ b/tests/Feature/TestCase.php @@ -38,7 +38,7 @@ protected function setUp(): void protected function getEnvironmentSetUp($app) { $app['config']->set('auth.providers.users.model', 'Playground\\Test\\Models\\User'); - $app['config']->set('playground.auth.verify', 'user'); + $app['config']->set('playground-auth.verify', 'user'); $app['config']->set('playground.load.routes', true); $app['config']->set('playground.routes.about', true); diff --git a/tests/Unit/Filters/ContentTrait/ContentModel.php b/tests/Unit/Filters/ContentTrait/ContentModel.php deleted file mode 100644 index 10c0859..0000000 --- a/tests/Unit/Filters/ContentTrait/ContentModel.php +++ /dev/null @@ -1,15 +0,0 @@ -assertSame($expected, $instance->purify($expected)); - } - - public function test_exorcise(): void - { - $instance = new ContentModel; - - $expected = 'some-string'; - - $this->assertSame($expected, $instance->exorcise($expected)); - } - - public function test_getHtmlPurifier(): void - { - $instance = new ContentModel; - - $this->assertInstanceOf( - \HTMLPurifier::class, - $instance->getHtmlPurifier() - ); - } - - public function test_getHtmlPurifier_with_iframes(): void - { - $instance = new ContentModel; - - $this->assertInstanceOf( - \HTMLPurifier::class, - $instance->getHtmlPurifier([ - 'iframes' => '%^(https?:)?(\/\/www\.youtube(?:-nocookie)?\.com\/embed\/|\/\/player\.vimeo\.com\/)%', - ]) - ); - } - - public function test_getHtmlPurifier_with_purifier_path(): void - { - $instance = new ContentModel; - - $this->assertInstanceOf( - \HTMLPurifier::class, - $instance->getHtmlPurifier([ - 'path' => '/tmp/purifier', - ]) - ); - } - - public function test_encodeURIComponent(): void - { - $instance = new ContentModel; - - $expected = 'some-string'; - - $this->assertSame($expected, $instance->encodeURIComponent($expected)); - } -} diff --git a/tests/Unit/Filters/ModelTrait/ArrayTraitTest.php b/tests/Unit/Filters/ModelTrait/ArrayTraitTest.php deleted file mode 100644 index 74c4828..0000000 --- a/tests/Unit/Filters/ModelTrait/ArrayTraitTest.php +++ /dev/null @@ -1,96 +0,0 @@ -assertSame([], $instance->filterArray([])); - - // NULL - $this->assertSame([], $instance->filterArray(null)); - - // false - $this->assertSame([], $instance->filterArray(false)); - - // true - $this->assertSame([], $instance->filterArray(true)); - - $value = 'just-a-string-value'; - - // A string is converted to any array. - $this->assertSame([$value], $instance->filterArray($value)); - - // A test array should not be altered. - $value = [ - 'i' => 'am-a-test-array', - 'someNullValue' => null, - 'aString' => 'thanks!', - 'object' => (object) ['ok' => true], - ]; - - // Returns the same array. - $this->assertSame($value, $instance->filterArray($value)); - } - - /** - * filterArrayToJson - * - * @see \Playground\Filters\ModelTrait::filterArrayToJson() - */ - public function test_filterArray_to_json(): void - { - $instance = new FilterModel; - - // Unexpected values return a json encoded empty array.. - - // empty array - $this->assertSame(json_encode([]), $instance->filterArrayToJson([])); - - // NULL - $this->assertSame(json_encode([]), $instance->filterArrayToJson(null)); - - // false - $this->assertSame(json_encode([]), $instance->filterArrayToJson(false)); - - // true - $this->assertSame(json_encode([]), $instance->filterArrayToJson(true)); - - $value = 'just-a-string-value'; - - // A string will remain a string, unchanged. - $this->assertSame($value, $instance->filterArrayToJson($value)); - - // A test array should not be altered. - $value = [ - 'i' => 'am-a-test-array', - 'someNullValue' => null, - 'aString' => 'thanks!', - 'object' => (object) ['ok' => true], - ]; - - // Returns the same array. - $this->assertSame(json_encode($value), $instance->filterArrayToJson($value)); - } -} diff --git a/tests/Unit/Filters/ModelTrait/BitsTraitTest.php b/tests/Unit/Filters/ModelTrait/BitsTraitTest.php deleted file mode 100644 index 9fd528a..0000000 --- a/tests/Unit/Filters/ModelTrait/BitsTraitTest.php +++ /dev/null @@ -1,64 +0,0 @@ -assertSame(0, $instance->filterBits(0)); - - $value = 1 + 2 + 4 + 8 + 16 + 32; - $expected = 1; - $this->assertSame($expected, $instance->filterBits($value)); - } - - /** - * filterBits: $exponent > 0 - * - * @see \Playground\Filters\ModelTrait::filterBits() - */ - public function test_filterBits_for_exponent_greater_than_zero(): void - { - $instance = new FilterModel; - - $value = 1 + 2 + 4 + 8 + 16 + 32; - - $exponent = 1; - $expected = 1 + 2; - $this->assertSame($expected, $instance->filterBits($value, $exponent)); - - $exponent = 2; - $expected = 1 + 2 + 4; - $this->assertSame($expected, $instance->filterBits($value, $exponent)); - - $exponent = 3; - $expected = 1 + 2 + 4 + 8; - $this->assertSame($expected, $instance->filterBits($value, $exponent)); - - $exponent = 4; - $expected = 1 + 2 + 4 + 8 + 16; - $this->assertSame($expected, $instance->filterBits($value, $exponent)); - - $exponent = 5; - $expected = 1 + 2 + 4 + 8 + 16 + 32; - $this->assertSame($expected, $instance->filterBits($value, $exponent)); - } -} diff --git a/tests/Unit/Filters/ModelTrait/DateTraitTest.php b/tests/Unit/Filters/ModelTrait/DateTraitTest.php deleted file mode 100644 index 3788699..0000000 --- a/tests/Unit/Filters/ModelTrait/DateTraitTest.php +++ /dev/null @@ -1,55 +0,0 @@ -assertNull($instance->filterDate('')); - - $PLAYGROUND_DATE_SQL = config('playground.date.sql'); - - if (! $PLAYGROUND_DATE_SQL || ! is_string($PLAYGROUND_DATE_SQL)) { - throw new \Exception('Expecting PLAYGROUND_DATE_SQL to be a string.'); - } - - $this->assertSame( - gmdate($PLAYGROUND_DATE_SQL, strtotime('now')), - $instance->filterDate('now') - ); - } - - /** - * filterDateAsCarbon - * - * @see \Playground\Filters\ModelTrait::filterDateAsCarbon() - */ - public function test_filterDateAsCarbon(): void - { - $instance = new FilterModel; - - $this->assertNull($instance->filterDateAsCarbon('')); - - $date = 'now'; - $this->assertInstanceOf(\DateTime::class, $instance->filterDateAsCarbon($date)); - $this->assertInstanceOf(\Carbon\Carbon::class, $instance->filterDateAsCarbon($date)); - } -} diff --git a/tests/Unit/Filters/ModelTrait/FilterModel.php b/tests/Unit/Filters/ModelTrait/FilterModel.php deleted file mode 100644 index b9b6bab..0000000 --- a/tests/Unit/Filters/ModelTrait/FilterModel.php +++ /dev/null @@ -1,15 +0,0 @@ -assertNull($instance->filterFloat('')); - $this->assertNull($instance->filterFloat(null)); - - $this->assertSame(0.0, $instance->filterFloat(0)); - $this->assertSame(1.0, $instance->filterFloat(1)); - } - - /** - * filterInteger - * - * @see \Playground\Filters\ModelTrait::filterInteger() - */ - public function test_filterInteger(): void - { - $instance = new FilterModel; - - $this->assertSame(0, $instance->filterInteger('')); - $this->assertSame(0, $instance->filterInteger(null)); - $this->assertSame(0, $instance->filterInteger(false)); - - // Needs i18n for numberformatter - // $this->assertSame(1000, $instance->filterInteger('1,000')); - // $this->assertSame(2000, $instance->filterInteger('2,000.01')); - $this->assertSame(0, $instance->filterInteger(0)); - $this->assertSame(1, $instance->filterInteger(1)); - $this->assertSame(-1001, $instance->filterInteger(-1001)); - } - - /** - * filterIntegerId - * - * @see \Playground\Filters\ModelTrait::filterIntegerId() - */ - public function test_filterIntegerId(): void - { - $instance = new FilterModel; - - $this->assertNull($instance->filterIntegerId('')); - $this->assertNull($instance->filterIntegerId(null)); - $this->assertNull($instance->filterIntegerId(false)); - - $this->assertNull($instance->filterIntegerId('1,000')); - $this->assertNull($instance->filterIntegerId('2,000.01')); - $this->assertNull($instance->filterIntegerId(0)); - $this->assertSame(1, $instance->filterIntegerId(1)); - $this->assertNull($instance->filterIntegerId(-1001)); - $this->assertNull($instance->filterIntegerId('-1001')); - $this->assertSame(1, $instance->filterIntegerId('1')); - $this->assertSame(100, $instance->filterIntegerId('100')); - $this->assertSame(2000000, $instance->filterIntegerId('2000000.0')); - } - - /** - * filterIntegerPositive - * - * @see \Playground\Filters\ModelTrait::filterIntegerPositive() - */ - public function test_filterIntegerPositive(): void - { - $instance = new FilterModel; - - $this->assertSame(0, $instance->filterIntegerPositive('')); - $this->assertSame(0, $instance->filterIntegerPositive(null)); - $this->assertSame(0, $instance->filterIntegerPositive(false)); - - $this->assertSame(1, $instance->filterIntegerPositive('1,000')); - $this->assertSame(2, $instance->filterIntegerPositive('2,000.01')); - $this->assertSame(0, $instance->filterIntegerPositive(0)); - $this->assertSame(1, $instance->filterIntegerPositive(1)); - $this->assertSame(1001, $instance->filterIntegerPositive(-1001)); - $this->assertSame(1001, $instance->filterIntegerPositive(-1001)); - - $absolute = true; - $this->assertSame(0, $instance->filterIntegerPositive('', $absolute)); - $this->assertSame(0, $instance->filterIntegerPositive(null, $absolute)); - $this->assertSame(0, $instance->filterIntegerPositive(false, $absolute)); - - $this->assertSame(1, $instance->filterIntegerPositive('1,000', $absolute)); - $this->assertSame(2, $instance->filterIntegerPositive('2,000.01', $absolute)); - $this->assertSame(0, $instance->filterIntegerPositive(0, $absolute)); - $this->assertSame(1, $instance->filterIntegerPositive(1, $absolute)); - $this->assertSame(1001, $instance->filterIntegerPositive(-1001, $absolute)); - $this->assertSame(1001, $instance->filterIntegerPositive(-1001, $absolute)); - - $absolute = false; - $this->assertSame(0, $instance->filterIntegerPositive('', $absolute)); - $this->assertSame(0, $instance->filterIntegerPositive(null, $absolute)); - $this->assertSame(0, $instance->filterIntegerPositive(false, $absolute)); - - $this->assertSame(1, $instance->filterIntegerPositive('1,000', $absolute)); - $this->assertSame(2, $instance->filterIntegerPositive('2,000.01', $absolute)); - $this->assertSame(0, $instance->filterIntegerPositive(0, $absolute)); - $this->assertSame(1, $instance->filterIntegerPositive(1, $absolute)); - $this->assertSame(-1001, $instance->filterIntegerPositive(-1001, $absolute)); - $this->assertSame(-1001, $instance->filterIntegerPositive(-1001, $absolute)); - } - - /** - * filterPercent - * - * @see \Playground\Filters\ModelTrait::filterPercent() - */ - public function test_filterPercent(): void - { - $instance = new FilterModel; - - $this->assertNull($instance->filterPercent('')); - $this->assertNull($instance->filterPercent(null)); - $this->assertNull($instance->filterPercent(false)); - - // $this->assertSame(1000.0, $instance->filterPercent('1,000%')); - - // $this->assertSame(1000.0, $instance->filterPercent('1,000')); - // $this->assertSame(2000.01, $instance->filterPercent('2,000.01')); - $this->assertSame(0.0, $instance->filterPercent(0)); - $this->assertSame(1.0, $instance->filterPercent(1)); - $this->assertSame(-1001.0, $instance->filterPercent('-1001 %')); - } -} diff --git a/tests/Unit/Filters/ModelTrait/StatusTraitTest.php b/tests/Unit/Filters/ModelTrait/StatusTraitTest.php deleted file mode 100644 index 7811d4b..0000000 --- a/tests/Unit/Filters/ModelTrait/StatusTraitTest.php +++ /dev/null @@ -1,47 +0,0 @@ -assertSame([], $instance->filterStatus([])); - - $expected = ['status' => 1]; - $this->assertSame($expected, $instance->filterStatus(['status' => 1])); - $this->assertSame($expected, $instance->filterStatus(['status' => '1'])); - $this->assertSame($expected, $instance->filterStatus(['status' => '-1.0'])); - - $expected = [ - 'status' => [ - 'active' => true, - 'lock' => true, - 'public' => false, - ], - ]; - $this->assertSame($expected, $instance->filterStatus([ - 'status' => [ - 'active' => 1, - 'lock' => true, - 'public' => 0, - ], - ])); - } -} diff --git a/tests/Unit/Filters/ModelTrait/SystemFieldsTraitTest.php b/tests/Unit/Filters/ModelTrait/SystemFieldsTraitTest.php deleted file mode 100644 index 334b8b3..0000000 --- a/tests/Unit/Filters/ModelTrait/SystemFieldsTraitTest.php +++ /dev/null @@ -1,105 +0,0 @@ -assertSame([], $instance->filterSystemFields([])); - - $expected = ['gids' => 1]; - $this->assertSame($expected, $instance->filterSystemFields(['gids' => 1])); - } - - /** - * filterSystemFields: gids - * - * @see \Playground\Filters\ModelTrait::filterSystemFields() - */ - public function test_filterSystemFields_for_permissions(): void - { - $instance = new FilterModel; - - $this->assertSame([], $instance->filterSystemFields([])); - - $expected = [ - 'po' => 7, - 'pg' => 4, - 'pw' => 4, - ]; - $this->assertSame($expected, $instance->filterSystemFields([ - 'po' => 7, - 'pg' => 4, - 'pw' => 4, - ])); - - // Only the permission bits may be set. - - $expected = [ - 'po' => 4, - 'pg' => 4, - 'pw' => 4, - ]; - $this->assertSame($expected, $instance->filterSystemFields([ - 'po' => 100, - 'pg' => 4, - 'pw' => 4, - ])); - } - - /** - * filterSystemFields: rank - * - * @see \Playground\Filters\ModelTrait::filterSystemFields() - */ - public function test_filterSystemFields_for_rank(): void - { - $instance = new FilterModel; - - $expected = ['rank' => 0]; - $this->assertSame($expected, $instance->filterSystemFields(['rank' => 0])); - - $expected = ['rank' => 1]; - $this->assertSame($expected, $instance->filterSystemFields(['rank' => 1])); - - $expected = ['rank' => -1]; - $this->assertSame($expected, $instance->filterSystemFields(['rank' => -1])); - } - - /** - * filterSystemFields: size - * - * @see \Playground\Filters\ModelTrait::filterSystemFields() - */ - public function test_filterSystemFields_for_size(): void - { - $instance = new FilterModel; - - $expected = ['size' => 0]; - $this->assertSame($expected, $instance->filterSystemFields(['size' => 0])); - - $expected = ['size' => 1]; - $this->assertSame($expected, $instance->filterSystemFields(['size' => 1])); - - $expected = ['size' => -1]; - $this->assertSame($expected, $instance->filterSystemFields(['size' => -1])); - } -} diff --git a/tests/Unit/Filters/ModelTrait/UuidTraitTest.php b/tests/Unit/Filters/ModelTrait/UuidTraitTest.php deleted file mode 100644 index 2b427d0..0000000 --- a/tests/Unit/Filters/ModelTrait/UuidTraitTest.php +++ /dev/null @@ -1,30 +0,0 @@ -uuid; - $this->assertNotEmpty($uuid); - $this->assertSame($uuid, $instance->filterUuid($uuid)); - } -} diff --git a/tests/Unit/Filters/ModelTrait/ValueTraitTest.php b/tests/Unit/Filters/ModelTrait/ValueTraitTest.php deleted file mode 100644 index 816bb64..0000000 --- a/tests/Unit/Filters/ModelTrait/ValueTraitTest.php +++ /dev/null @@ -1,108 +0,0 @@ -assertFalse($instance->filterBoolean([])); - - // NULL - $this->assertFalse($instance->filterBoolean(null)); - - // false - $this->assertFalse($instance->filterBoolean([])); - - // true - $this->assertTrue($instance->filterBoolean(true)); - - // Positive numbers are true - $this->assertTrue($instance->filterBoolean(10)); - $this->assertTrue($instance->filterBoolean(1)); - $this->assertFalse($instance->filterBoolean(0)); - $this->assertFalse($instance->filterBoolean(-10)); - - // Empty string - $this->assertFalse($instance->filterBoolean('')); - - $this->assertTrue($instance->filterBoolean('true')); - - $this->assertFalse($instance->filterBoolean('not-true')); - - $value = [ - 'i' => 'am-a-test-array', - 'someNullValue' => null, - 'aString' => 'thanks!', - 'object' => (object) ['ok' => true], - ]; - - $this->assertTrue($instance->filterBoolean($value)); - } - - /** - * filterEmail - * - * @see \Playground\Filters\ModelTrait::filterEmail() - */ - public function test_filterEmail(): void - { - $instance = new FilterModel; - - $value = false; - $this->assertSame('', $instance->filterEmail($value)); - - $value = 'not valid email with spaces @ example.com'; - $this->assertSame('notvalidemailwithspaces@example.com', $instance->filterEmail($value)); - - $value = 'test@'; - $this->assertSame($value, $instance->filterEmail($value)); - - $value = 'test@example.com'; - $this->assertSame($value, $instance->filterEmail($value)); - $value = ''; - $this->assertSame('test@example.com', $instance->filterEmail($value)); - - $value = 'test+with.plus-addressing@example.com'; - $this->assertSame($value, $instance->filterEmail($value)); - } - - /** - * filterHtml - * - * @see \Playground\Filters\ContentTrait::purify() HTMLPurifier - * @see \Playground\Filters\ModelTrait::filterHtml() - */ - public function test_filterHtml(): void - { - $instance = new FilterModel; - - $value = 'Tags should be removed.'; - $this->assertSame('Tags should be removed.', $instance->filterHtml($value)); - - $value = 'No links allowed either.'; - $this->assertSame('No links allowed either.', $instance->filterHtml($value)); - } -} diff --git a/tests/Unit/Models/Model/ModelTest.php b/tests/Unit/Models/Model/ModelTest.php index e22c624..03c20ac 100644 --- a/tests/Unit/Models/Model/ModelTest.php +++ b/tests/Unit/Models/Model/ModelTest.php @@ -32,7 +32,7 @@ protected function setUp(): void static::MODEL_CLASS )); } - config(['playground.user' => \Playground\Test\Models\User::class]); + config(['auth.providers.users.model' => \Playground\Test\Models\User::class]); } public function test_WithChildren_children_returns_HasMany(): void diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index dbb9255..af1efb4 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -30,6 +30,6 @@ protected function getPackageProviders($app) protected function getEnvironmentSetUp($app) { $app['config']->set('auth.providers.users.model', 'Playground\\Test\\Models\\User'); - $app['config']->set('playground.auth.verify', 'user'); + $app['config']->set('playground-auth.verify', 'user'); } }