Source of file GridFieldClearAllButtonTest.php
Size: 3,846 Bytes - Last Modified: 2021-12-23T10:24:51+00:00
/var/www/docs.ssmods.com/process/src/tests/Forms/GridField/GridFieldClearAllButtonTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | <?php namespace SilverLeague\LogViewer\Tests\Forms\GridField; use SilverLeague\LogViewer\Forms\GridField\GridFieldClearAllButton; use SilverLeague\LogViewer\Model\LogEntry; use SilverStripe\Control\Controller; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\FixtureFactory; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\ORM\DataList; /** * Tests for the "clear all" GridField action class * * @package silverstripe-logviewer * @author Robbie Averill <robbie@averill.co.nz> */ class GridFieldClearAllButtonTest extends SapphireTest { /** * {@inheritDoc} */ protected $usesDatabase = true; /** * @var GridField */ protected $gridField; /** * {@inheritDoc} */ public function setUp() { parent::setUp(); $config = GridFieldConfig::create()->addComponent(new GridFieldClearAllButton('before')); $this->gridField = GridField::create('logs', 'logs', DataList::create(LogEntry::class), $config); $form = Form::create( null, 'foobar', FieldList::create([$this->gridField]), FieldList::create() ); } /** * Return the actual class we're testing * * @return GridFieldClearAllButton */ protected function getSubject() { return $this->gridField->getConfig()->getComponentByType(GridFieldClearAllButton::class); } /** * Ensure that the HTML fragment was pushed correctly and assigned to the specified fragment (in setUp above) */ public function testGetHtmlFragments() { $fragments = $this->getSubject()->getHTMLFragments($this->gridField); $this->assertArrayHasKey('before', $fragments); $this->assertContains('Clear all', $fragments['before']); $this->assertContains('clear-all-logs', $fragments['before']); $this->assertContains('font-icon-trash-bin action_clear', $fragments['before']); } /** * Test that the GridFieldAction actions are returned correctly */ public function testActionsAreDefined() { $this->assertSame(['clear'], (new GridFieldClearAllButton)->getActions($this->gridField)); } /** * Test that an exception is thrown if the Member doesn't have permission to delete the data class assigned * * @expectedException \SilverStripe\ORM\ValidationException * @expectedExceptionMessage No permission to unlink record */ public function testCannotClearAllWithoutPermission() { $forbiddenList = DataList::create(Stub\SomeDataObject::class); $this->gridField->setList($forbiddenList); $this->getSubject()->handleAction($this->gridField, 'clear', null, []); } /** * Test that with permission the list can be cleared */ public function testClearList() { $this->logInWithPermission('ADMIN'); $this->createDummyLogs(); $this->assertSame(5, $this->gridField->getList()->count()); $this->getSubject()->handleAction($this->gridField, 'clear', null, []); $this->assertSame(0, $this->gridField->getList()->count()); } /** * Create a set of dummy LogEntry records * * @param int $limit */ protected function createDummyLogs($limit = 5) { $factory = Injector::inst()->create(FixtureFactory::class); for ($i = 1; $i <= $limit; $i++) { $factory->createObject( LogEntry::class, 'stub_log_' . $i, [ 'Entry' => 'Log #' . $i, 'Level' => 'DEBUG' ] ); } } } |