Skip to content

Commit

Permalink
Merge pull request #5594 from Laravel-Backpack/add-misc-tests
Browse files Browse the repository at this point in the history
Remove DB dependency from column tests
  • Loading branch information
pxpm authored Aug 2, 2024
2 parents cd321da + 6c343fb commit fe0ac36
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/app/Library/CrudPanel/Traits/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ public function makeSureColumnHasNeededAttributes($column)
{
$column = $this->makeSureColumnHasName($column);
$column = $this->makeSureColumnHasKey($column);
$column = $this->makeSureColumnHasLabel($column);
$column = $this->makeSureColumnHasEntity($column);
$column = $this->makeSureColumnHasLabel($column);
$column = $this->makeSureColumnHasModel($column);
$column = $this->makeSureColumnHasAttribute($column);
$column = $this->makeSureColumnHasRelationType($column);
Expand Down
7 changes: 3 additions & 4 deletions src/app/Library/CrudPanel/Traits/ColumnsProtectedMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ protected function makeSureColumnHasKey($column)
}

/**
* If a column definition is missing the wrapper element, set the default (empty).
* The wrapper is the HTML element that wrappes around the column text.
* By defining this array a developer can wrap the text into an anchor (link),
* span, div or whatever they want.
* @deprecated Never used. Will be removed in a future version.
*
* @param array $column Column definition array.
* @return array Column definition array with wrapper.
*
* @codeCoverageIgnore
*/
protected function makeSureColumnHasWrapper($column)
{
Expand Down
10 changes: 10 additions & 0 deletions tests/BaseTestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ protected function setupUserCreateRequest()
$this->crudPanel->setRequest($request);
}

protected function makeAnArticleModel(array $attributes = [])
{
$attributes = array_merge([
'id' => 1,
'content' => 'Some Content',
], $attributes);

return \Backpack\CRUD\Tests\config\Models\Article::make($attributes);
}

// allow us to run crud panel private/protected methods like `inferFieldTypeFromDbColumnType`
public function invokeMethod(&$object, $methodName, array $parameters = [])
{
Expand Down
76 changes: 64 additions & 12 deletions tests/Unit/CrudPanel/CrudPanelColumnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backpack\CRUD\Tests\Unit\CrudPanel;

use Backpack\CRUD\app\Library\CrudPanel\CrudColumn;
use Backpack\CRUD\Tests\config\Models\Article;
use Backpack\CRUD\Tests\config\Models\User;

/**
Expand All @@ -12,7 +11,7 @@
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudColumn
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudPanel
*/
class CrudPanelColumnsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel
class CrudPanelColumnsTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel
{
private $oneColumnArray = [
'name' => 'column1',
Expand Down Expand Up @@ -597,13 +596,66 @@ public function testOrderColumnsMixedList()
$this->assertEquals(['column2', 'column1', 'column3'], array_keys($this->crudPanel->columns()));
}

public function testItDoesNotAttemptToGetEntityWhenColumnNameIsArray()
{
$this->crudPanel->addColumn(['name' => ['test1', 'test2'], 'label' => 'Column1', 'type' => 'text', 'key' => 'column1']);
$this->assertArrayNotHasKey('entity', $this->crudPanel->firstColumnWhere('key', 'column1'));
}

public function testItCanInferTheEntityFromColumnNameUsingEntity_idConvention()
{
$this->crudPanel->addColumn('article_id');

$this->assertEquals('article', $this->crudPanel->firstColumnWhere('name', 'article_id')['entity']);
}

public function testItAlwaysHasDatabaseColumnWhenDriverIsNotSql()
{
$this->crudPanel = new \Backpack\CRUD\Tests\config\CrudPanel\NoSqlDriverCrudPanel();
$this->crudPanel->setModel(User::class);

$this->assertTrue($this->invokeMethod($this->crudPanel, 'hasDatabaseColumn', ['test', 'test']));
}

public function testItCanGetTheColumnTypeFromModelCasts()
{
$this->crudPanel->addColumn('arrayCast');
$this->crudPanel->addColumn('jsonCast');
$this->crudPanel->addColumn('dateCast');
$this->crudPanel->addColumn('booleanCast');
$this->crudPanel->addColumn('datetimeCast');
$this->crudPanel->addColumn('numberCast');

$this->assertEquals('array', $this->crudPanel->firstColumnWhere('name', 'arrayCast')['type']);
$this->assertEquals('json', $this->crudPanel->firstColumnWhere('name', 'jsonCast')['type']);
$this->assertEquals('date', $this->crudPanel->firstColumnWhere('name', 'dateCast')['type']);
$this->assertEquals('check', $this->crudPanel->firstColumnWhere('name', 'booleanCast')['type']);
$this->assertEquals('datetime', $this->crudPanel->firstColumnWhere('name', 'datetimeCast')['type']);
$this->assertEquals('number', $this->crudPanel->firstColumnWhere('name', 'numberCast')['type']);
}

public function testItCanGetTheColumnTypeFromModelDates()
{
$this->crudPanel->addColumn('created_at');

$this->assertEquals('datetime', $this->crudPanel->firstColumnWhere('name', 'created_at')['type']);
}

public function testMakeFirstColumnReturnFalseWhenNoColumnsExist()
{
$this->assertEmpty($this->crudPanel->columns());
$column = $this->crudPanel->makeFirstColumn();
$this->assertFalse($column);
}

public function testItSetsTextColumnTypeForTranslatableColumns()
{
$this->crudPanel->setModel(\Backpack\CRUD\Tests\config\Models\TestModelWithTranslations::class);
$this->crudPanel->addColumn('translatableColumn');

$this->assertEquals('text', $this->crudPanel->firstColumnWhere('name', 'translatableColumn')['type']);
}

public function testItCanAddADefaultTypeToTheColumn()
{
$column = $this->crudPanel->addDefaultTypeToColumn(['name' => 'name']);
Expand Down Expand Up @@ -737,7 +789,7 @@ public function testColumnLinkToWithRouteNameOnly()
$columnArray = $this->crudPanel->columns()['articles'];
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']);
$arguments = $reflection->getClosureUsedVariables();
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('articles.show', $arguments['route']);
$this->assertCount(1, $arguments['parameters']);
Expand All @@ -752,7 +804,7 @@ public function testColumnLinkToWithRouteNameAndAdditionalParameters()
$arguments = $reflection->getClosureUsedVariables();
$this->assertEquals('articles.show', $arguments['route']);
$this->assertCount(3, $arguments['parameters']);
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url);
}
Expand All @@ -761,7 +813,7 @@ public function testColumnLinkToWithCustomParameters()
{
$this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']);
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url);
}
Expand All @@ -772,7 +824,7 @@ public function testColumnLinkToWithCustomClosureParameters()
->entity('articles')
->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]);
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url);
}
Expand All @@ -783,7 +835,7 @@ public function testColumnLinkToDontAutoInferParametersIfAllProvided()
->entity('articles')
->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]);
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url);
}
Expand All @@ -794,7 +846,7 @@ public function testColumnLinkToAutoInferAnySingleParameter()
->entity('articles')
->linkTo('article.show.detail', ['id' => 123, 'otherParam' => fn ($entry) => $entry->content]);
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url);
}
Expand All @@ -805,7 +857,7 @@ public function testColumnLinkToWithClosure()
->entity('articles')
->linkTo(fn ($entry) => route('articles.show', $entry->content));
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url);
}
Expand All @@ -819,7 +871,7 @@ public function testColumnArrayDefinitionLinkToRouteAsClosure()
'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']),
]);
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url);
}
Expand All @@ -833,7 +885,7 @@ public function testColumnArrayDefinitionLinkToRouteNameOnly()
'linkTo' => 'articles.show',
]);
$columnArray = $this->crudPanel->columns()['articles'];
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/1/show', $url);
}
Expand All @@ -857,7 +909,7 @@ public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters(
$arguments = $reflection->getClosureUsedVariables();
$this->assertEquals('articles.show', $arguments['route']);
$this->assertCount(3, $arguments['parameters']);
$this->crudPanel->entry = Article::first();
$this->crudPanel->entry = $this->makeAnArticleModel();
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1);
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=Some%20Content', $url);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/config/Models/AccountDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class AccountDetails extends Model
protected $table = 'account_details';
protected $fillable = ['user_id', 'nickname', 'profile_picture', 'article_id', 'start_date', 'end_date'];

public function identifiableAttribute()
{
return 'nickname';
}

/**
* Get the user for the account details.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/config/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Article extends Model
use CrudTrait;

protected $table = 'articles';
protected $fillable = ['user_id', 'content', 'metas', 'tags', 'extras', 'cast_metas', 'cast_tags', 'cast_extras'];
protected $fillable = ['id', 'user_id', 'content', 'metas', 'tags', 'extras', 'cast_metas', 'cast_tags', 'cast_extras'];
protected $casts = [
'cast_metas' => 'object',
'cast_tags' => 'object',
Expand Down
18 changes: 18 additions & 0 deletions tests/config/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,26 @@ class TestModel extends \Illuminate\Database\Eloquent\Model
{
use CrudTrait;

protected $casts = [
'arrayCast' => 'array',
'jsonCast' => 'json',
'dateCast' => 'date',
'booleanCast' => 'boolean',
'datetimeCast' => 'datetime',
'numberCast' => 'timestamp',
];

protected $dates = [
'someDate',
];

public function buttonModelFunction()
{
return 'model function button test';
}

public function article()
{
return $this->belongsTo('Backpack\CRUD\Tests\Config\Models\Article');
}
}
16 changes: 16 additions & 0 deletions tests/config/Models/TestModelWithTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Backpack\CRUD\Tests\Config\Models;

class TestModelWithTranslations extends TestModel
{
public function translationEnabledForModel()
{
return true;
}

public function getTranslations()
{
return ['translatableColumn' => null];
}
}
5 changes: 5 additions & 0 deletions tests/config/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class User extends Model

protected $fillable = ['name', 'email', 'password', 'extras'];

public function identifiableAttribute()
{
return 'name';
}

/**
* Get the account details associated with the user.
*/
Expand Down

0 comments on commit fe0ac36

Please sign in to comment.