Skip to content

Commit

Permalink
Merge pull request #5567 from Laravel-Backpack/add-miscelaneous-tests
Browse files Browse the repository at this point in the history
Fix CrudPanel button() when instantiating with an array of attributes
  • Loading branch information
pxpm authored Jul 18, 2024
2 parents e40e01b + 102d80a commit fd8ea6d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/app/Library/CrudPanel/CrudButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public function __construct($nameOrAttributes, $stack = null, $type = null, $con
extract($nameOrAttributes);
}

$this->name = $nameOrAttributes ?? 'button_'.rand(1, 999999999);
// if $name was not extracted and there is no string to use as name, generate a random one
$name ??= is_string($nameOrAttributes) ? $nameOrAttributes : 'button_'.rand(1, 999999999);

$this->name = $name;
$this->stack = $stack ?? 'top';
$this->type = $type ?? 'view';
$this->content = $content;
Expand Down
90 changes: 85 additions & 5 deletions tests/Unit/CrudPanel/CrudPanelButtonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,24 @@ public function testOrderButtons()
$this->assertEquals(['show', 'test', 'update'], $this->crudPanel->buttons()->pluck('name')->toArray());
}

public function testOrderButtonsInStack()
{
$this->addDefaultButtons();

$this->crudPanel->orderButtons('top', ['topModelFunctionButton']);

$this->assertEquals(['topModelFunctionButton', 'topViewButton'], $this->crudPanel->getButtonsForStack('top')->pluck('name')->toArray());
}

public function testOrderThrowExceptionIfButtonDoesNotExist()
{
$this->addDefaultButtons();

$this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);

$this->crudPanel->orderButtons('top', ['unknownButton']);
}

public function testAddButtonFluently()
{
$button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view');
Expand All @@ -266,6 +284,68 @@ public function testAddButtonFluently()
$this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray());
}

public function testItThrowsExceptionWhenModifyingUnknownButton()
{
$this->addDefaultButtons();

$this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);

$this->crudPanel->modifyButton('unknownButton', function ($button) {
$button->name = 'newName';
});
}

public function testItCanAddAButtonFromAModelFunction()
{
$this->crudPanel->addButtonFromModelFunction('line', 'buttonModelFunction', 'buttonModelFunction');
$this->assertEquals('buttonModelFunction', $this->crudPanel->buttons()->first()->content);
}

public function testItDoesNotMoveFieldWhenTargetIsUnknown()
{
$this->addDefaultButtons();

$firstButtonName = $this->crudPanel->buttons()->first()->name;

$this->crudPanel->moveButton('unknownButton', 'before', 'topViewButton');

$this->assertCount(4, $this->crudPanel->buttons());
$this->assertEquals($firstButtonName, $this->crudPanel->buttons()->first()->name);
}

public function testItDoesNotMoveButtonWhenDestinationIsUnknown()
{
$this->addDefaultButtons();

$firstButtonName = $this->crudPanel->buttons()->first()->name;

$this->crudPanel->moveButton('topViewButton', 'before', 'unknownButton');

$this->assertCount(4, $this->crudPanel->buttons());
$this->assertEquals($firstButtonName, $this->crudPanel->buttons()->first()->name);
}

public function testItCanCreateANewCrudButtonInstance()
{
$button = $this->crudPanel->button(['name' => 'testButton', 'stack' => 'line', 'type' => 'view', 'content' => 'crud::buttons.test']);
$this->assertEquals($button->toArray(), $this->crudPanel->buttons()->last()->toArray());
$this->assertInstanceOf(\Backpack\CRUD\app\Library\CrudPanel\CrudButton::class, $button);
}

public function testItCanCheckIfAnyOfTheButtonsHasTheDeterminedKayValuePair()
{
$this->addDefaultButtons();

$this->assertTrue($this->crudPanel->hasButtonWhere('name', 'topViewButton'));
$this->assertFalse($this->crudPanel->hasButtonWhere('name', 'unknownButton'));
}

public function testItGenerateARandomButtonNameIfOneNotProvided()
{
$button = $this->crudPanel->button(['stack' => 'line', 'type' => 'view', 'content' => 'crud::buttons.test']);
$this->assertTrue(str_starts_with($button->name, 'button_'));
}

private function getButtonByName($name)
{
return $this->crudPanel->buttons()->first(function ($value) use ($name) {
Expand All @@ -275,15 +355,15 @@ private function getButtonByName($name)

private function addDefaultButtons()
{
CrudButton::name($this->topViewButton);
CrudButton::name($this->lineViewButton);
CrudButton::name($this->bottomViewButton);
CrudButton::name($this->topModelFunctionButton);
$this->crudPanel->button($this->topViewButton);
$this->crudPanel->button($this->lineViewButton);
$this->crudPanel->button($this->bottomViewButton);
$this->crudPanel->button($this->topModelFunctionButton);
}

private function addTestButton($buttonName)
{
CrudButton::name(array_values($this->{$buttonName}));
$this->crudPanel->button(array_values($this->{$buttonName}));
}

public function testMovingTheButtonUsingPosition()
Expand Down
5 changes: 5 additions & 0 deletions tests/config/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
class TestModel extends \Illuminate\Database\Eloquent\Model
{
use CrudTrait;

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

0 comments on commit fd8ea6d

Please sign in to comment.