1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gurucomkz\EagerLoading\Tests; |
4
|
|
|
|
5
|
|
|
use Gurucomkz\EagerLoading\GridFieldEagerLoadManipulator; |
6
|
|
|
use Gurucomkz\EagerLoading\Tests\Admin\TestModelAdmin; |
7
|
|
|
use Gurucomkz\EagerLoading\Tests\Models\Drink; |
8
|
|
|
use Gurucomkz\EagerLoading\Tests\Models\Music; |
9
|
|
|
use Gurucomkz\EagerLoading\Tests\Models\Origin; |
10
|
|
|
use Gurucomkz\EagerLoading\Tests\Models\Player; |
11
|
|
|
use Gurucomkz\EagerLoading\Tests\Models\Team; |
12
|
|
|
use SilverStripe\Control\HTTPRequest; |
13
|
|
|
use SilverStripe\Control\Session; |
14
|
|
|
use SilverStripe\Core\Injector\Injector; |
15
|
|
|
use SilverStripe\Dev\FunctionalTest; |
16
|
|
|
use SilverStripe\Forms\GridField\FormAction\StateStore; |
17
|
|
|
use SilverStripe\Security\Permission; |
18
|
|
|
|
19
|
|
|
class EagerloadAdminTest extends FunctionalTest |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
protected static $fixture_file = 'fixtures.yml'; |
23
|
|
|
|
24
|
|
|
protected static $extra_dataobjects = [ |
25
|
|
|
Player::class, |
26
|
|
|
Origin::class, |
27
|
|
|
Music::class, |
28
|
|
|
Team::class, |
29
|
|
|
Drink::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
protected static $extra_controllers = [ |
33
|
|
|
TestModelAdmin::class, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function testModelAdminOpens() |
37
|
|
|
{ |
38
|
|
|
$this->autoFollowRedirection = false; |
39
|
|
|
$this->logInAs('admin'); |
40
|
|
|
$this->assertTrue((bool)Permission::check("ADMIN")); |
41
|
|
|
$this->assertEquals(200, $this->get('admin/eltestadmin')->getStatusCode()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testAdminContansExtension() |
45
|
|
|
{ |
46
|
|
|
$admin = new TestModelAdmin(); |
47
|
|
|
$request = new HTTPRequest('GET', '/'); |
48
|
|
|
$request->setSession(new Session([])); |
49
|
|
|
$admin->setRequest($request); |
50
|
|
|
$admin->doInit(); |
51
|
|
|
|
52
|
|
|
$form = $admin->getEditForm(); |
53
|
|
|
$field = $form->Fields()->fieldByName('Gurucomkz-EagerLoading-Tests-Models-Player'); |
54
|
|
|
$this->assertNotNull($field, 'A GridField has been found on the form.'); |
55
|
|
|
|
56
|
|
|
$config = $field->getConfig(); |
57
|
|
|
|
58
|
|
|
$this->assertNotNull( |
59
|
|
|
$config->getComponentByType(GridFieldEagerLoadManipulator::class), |
60
|
|
|
'GridFieldEagerLoadManipulator added' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testAdminExport() |
65
|
|
|
{ |
66
|
|
|
$exportActionStateID = $this->getActionSessionKey('export'); |
67
|
|
|
$exportActionText = _t('SilverStripe\\Forms\\GridField\\GridField.CSVEXPORT', 'Export to CSV'); |
68
|
|
|
$getVars = [ |
69
|
|
|
'action_gridFieldAlterAction?StateID=' . $exportActionStateID => $exportActionText, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$this->autoFollowRedirection = false; |
73
|
|
|
$this->logInAs('admin'); |
74
|
|
|
$this->assertTrue((bool)Permission::check("ADMIN")); |
75
|
|
|
$modelClassName = "Gurucomkz-EagerLoading-Tests-Models-Player"; |
76
|
|
|
$getLink = "admin/eltestadmin/$modelClassName/EditForm/field/$modelClassName?" . http_build_query($getVars); |
77
|
|
|
|
78
|
|
|
$this->assertEquals(200, $this->get($getLink)->getStatusCode()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getActionSessionKey($action) |
82
|
|
|
{ |
83
|
|
|
$this->autoFollowRedirection = false; |
84
|
|
|
$this->logInAs('admin'); |
85
|
|
|
$modelClassName = "Gurucomkz-EagerLoading-Tests-Models-Player"; |
86
|
|
|
$getLink = "admin/eltestadmin/$modelClassName/EditForm/field/$modelClassName"; |
87
|
|
|
|
88
|
|
|
$body = $this->get($getLink)->getBody(); |
89
|
|
|
|
90
|
|
|
preg_match_all('/"action_gridFieldAlterAction\?StateID=([^"]+)"/', $body, $matches); |
91
|
|
|
|
92
|
|
|
/** @var StateStore $store */ |
93
|
|
|
$store = Injector::inst()->create(StateStore::class . '.' . 'Gurucomkz-EagerLoading-Tests-Models-Player'); |
94
|
|
|
|
95
|
|
|
foreach ($matches[1] as $actionCode) { |
96
|
|
|
$actionData = $store->load($actionCode); |
97
|
|
|
if ($action == $actionData['actionName']) { |
98
|
|
|
return $actionCode; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|