Source of file ModelAdminTest.php
Size: 9,237 Bytes - Last Modified: 2021-12-23T10:27:20+00:00
/var/www/docs.ssmods.com/process/src/tests/php/ModelAdminTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 | <?php namespace SilverStripe\Admin\Tests; use SilverStripe\Admin\Tests\ModelAdminTest\Contact; use SilverStripe\Admin\Tests\ModelAdminTest\MultiModelAdmin; use SilverStripe\Admin\Tests\ModelAdminTest\Player; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\Session; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldExportButton; use SilverStripe\Forms\GridField\GridFieldImportButton; use SilverStripe\Forms\GridField\GridFieldPrintButton; use SilverStripe\Security\Permission; use SilverStripe\Dev\FunctionalTest; use SilverStripe\View\ArrayData; class ModelAdminTest extends FunctionalTest { protected static $fixture_file = 'ModelAdminTest.yml'; protected static $extra_dataobjects = [ ModelAdminTest\Contact::class, ModelAdminTest\Player::class, ModelAdminTest\OverridenModelAdmin::class ]; protected static $extra_controllers = [ ModelAdminTest\ContactAdmin::class, ModelAdminTest\PlayerAdmin::class, ]; public function testModelAdminOpens() { $this->autoFollowRedirection = false; $this->logInAs('admin'); $this->assertTrue((bool)Permission::check("ADMIN")); $this->assertEquals(200, $this->get('ContactAdmin')->getStatusCode()); } public function testMultiModelAdminOpensEachTab() { $admin = new MultiModelAdmin(); $this->logInWithPermission(); foreach ($admin->getManagedModelTabs()->toNestedArray() as $tab) { $request = new HTTPRequest('GET', $tab['Link']); $request->setRouteParams(['ModelClass' => substr($tab['Link'], strlen('admin/multi/'))]); $request->setSession(new Session([])); Injector::inst()->registerService($request, HTTPRequest::class); $admin->setRequest($request); $admin->doInit(); $this->assertEquals( $tab["Tab"], $admin->getModelTab(), sprintf('Accessing the %s link resolves to the %s tab', $tab['Link'], $tab["Tab"]) ); $this->assertEquals( $tab["ClassName"], $admin->getModelClass(), sprintf( 'Accessing the %s link sets the ModelAdmin class to %s', $tab['Link'], $tab["ClassName"] ) ); } } public function testExportFieldsDefaultIsSummaryFields() { $admin = new ModelAdminTest\ContactAdmin(); $request = new HTTPRequest('GET', '/'); $request->setSession(new Session([])); $admin->setRequest($request); $admin->doInit(); $this->assertEquals( $admin->getExportFields(), ModelAdminTest\Contact::singleton()->summaryFields() ); } public function testExportFieldsOverloadedMethod() { $admin = new ModelAdminTest\PlayerAdmin(); $request = new HTTPRequest('GET', '/'); $request->setSession(new Session([])); $admin->setRequest($request); $admin->doInit(); $this->assertEquals($admin->getExportFields(), array( 'Name' => 'Name', 'Position' => 'Position' )); } public function testGetGridField() { $admin = new ModelAdminTest\OverridenModelAdmin(); $request = new HTTPRequest('GET', '/'); $request->setSession(new Session([])); $admin->setRequest($request); $admin->doInit(); // Test the call counts $this->assertEquals( [ 'getGridField' => 0, 'getGridFieldConfig' => 0, 'updateGridField' => 0, 'updateGridFieldConfig' => 0, ], $admin->calls, 'Before calling Edit Form, all our call counts are zero' ); $form = $admin->getEditForm(); $this->assertEquals( [ 'getGridField' => 1, 'getGridFieldConfig' => 1, 'updateGridField' => 1, 'updateGridFieldConfig' => 1, ], $admin->calls, 'All protected method and extension points have been called once.' ); // Test the GridField generation /** @var GridField $field */ $field = $form->Fields()->fieldByName('SilverStripe-Admin-Tests-ModelAdminTest-Player'); $this->assertNotNull($field, 'A GridField has been found on the form.'); $this->assertStringContainsString( 'OverridenModelAdmin', $field->extraClass(), 'OverridenModelAdmin has added an extra class to the grid field' ); $this->assertStringContainsString( 'called', $field->getAttribute('ModelAdminExtension'), 'ModelAdminExtension has added an attribute to the GridField' ); // Test the GridField Config $config = $field->getConfig(); $this->assertEmpty( $config->getComponentByType(GridFieldExportButton::class), 'OverridenModelAdmin removes the Export Button' ); $this->assertEmpty( $config->getComponentByType(GridFieldPrintButton::class), 'ModelAdminExtension removes the Print Button' ); $this->assertNotEmpty( $config->getComponentByType(GridFieldImportButton::class), 'The import button is still there' ); } public function testGetManagedModels() { $admin = new ModelAdminTest\MultiModelAdmin(); $models = $admin->getManagedModels(); $this->assertEquals( [ 'dataClass' => Contact::class, 'title' => 'Contacts' ], $models[Contact::class], 'Managed Model that are class name only get normalised' ); $this->assertEquals( [ 'dataClass' => Player::class, 'title' => 'Ice Hockey Players' ], $models['Player'], 'Managed Model with an explicit dataClass can have a custom URL key' ); $this->assertEquals( [ 'dataClass' => Player::class, 'title' => 'Rugby Players' ], $models[Player::class], 'Managed Model without a dataClass provided default to using the class name for dataClass' ); $this->assertEquals( [ 'dataClass' => Player::class, 'title' => 'Cricket Players', ], $models['cricket-players'], 'Managed Model with an arbitrary name can have a hyphen in the URL key' ); } public function testGetManagedModelTabs() { $mock = $this->getMockBuilder(ModelAdminTest\MultiModelAdmin::class) ->setMethods(['getManagedModels']) ->getMock(); // `getManagedModelTabs` relies on `getManagedModels` whose output format has changed within the 4.x line. // We need to mock `getManagedModels` so it returns both the legacy and updated format. $mock->expects($this->once()) ->method('getManagedModels') ->will($this->returnValue([ 'Player' => [ 'dataClass' => Player::class, 'title' => 'Ice Hockey Players' ], Player::class => [ 'title' => 'Rugby Players' ], 'cricket-players' => [ 'dataClass' => Player::class, 'title' => 'Cricket Players', ], ])); $tabs = $mock->getManagedModelTabs()->toNestedArray(); $this->assertEquals( [ 'Title' => 'Ice Hockey Players', 'Tab' => 'Player', 'ClassName' => Player::class, 'Link' => 'admin/multi/Player', 'LinkOrCurrent' => 'link', ], $tabs[0], 'Tab data for managed model array using the newer syntax with dataClass can be generated' ); $this->assertEquals( [ 'Title' => 'Rugby Players', 'Tab' => 'SilverStripe\Admin\Tests\ModelAdminTest\Player', 'ClassName' => Player::class, 'Link' => 'admin/multi/SilverStripe-Admin-Tests-ModelAdminTest-Player', 'LinkOrCurrent' => 'link', ], $tabs[1], 'Tab data for managed model array using the older syntax without dataClass can be generated' ); $this->assertEquals( [ 'Title' => 'Cricket Players', 'Tab' => 'cricket-players', 'ClassName' => Player::class, 'Link' => 'admin/multi/cricket-players', 'LinkOrCurrent' => 'link', ], $tabs[2], 'Tab data for managed model array using the newer syntax with dataClass and a hyphen can be generated' ); } } |