Skip to content

Commit

Permalink
Add test for Ajax extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Aug 3, 2023
1 parent 1e249a4 commit eea80c7
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/php/Extensions/AjaxFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace SilverStripe\AnyField\Tests\Extensions;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\AnyField\Extensions\AjaxField;
use SilverStripe\CMS\Forms\AnchorSelectorField;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\LinkField\Models\SiteTreeLink;

class AjaxFieldTest extends SapphireTest
{

protected $use_database = false;

public function testExtensionApplied()
{
$field = TreeDropdownField::create('Name', 'Label', SiteTree::class);
$this->assertTrue($field->hasExtension(AjaxField::class), 'AjaxField is applied to TreeDropdownField');

$field = AnchorSelectorField::create('Name');
$this->assertTrue($field->hasExtension(AjaxField::class), 'AjaxField is applied to TreeDropdownField');
}

public function testUpdateLinkModalForm()
{
$request = new HTTPRequest(
'GET',
'admin/methodSchema/Modals/AnyFieldForm',
['key' => SiteTreeLink::class],
[]
);
$request->setSession(new Session([]));
$leftAndMain = LeftAndMain::create();
$controller = $leftAndMain->Modals();
$leftAndMain->setRequest($request);

$field = TreeDropdownField::create('Name', 'Label', SiteTree::class);

Form::create(
$controller,
'Modals/AnyFieldForm',
FieldList::create($field)
);

$this->assertEquals(
'admin/Modals/Modals/AnyFieldForm/field/Name?key=' . urlencode(SiteTreeLink::class),
$field->Link(),
'Link is updated with the key'
);
}

public function testDontUpdateLinkModalForm()
{
$request = new HTTPRequest(
'GET',
'admin',
['key' => SiteTreeLink::class],
[]
);
$request->setSession(new Session([]));
$leftAndMain = LeftAndMain::create();
$leftAndMain->setRequest($request);

$field = TreeDropdownField::create('Name', 'Label', SiteTree::class);

Form::create(
$leftAndMain,
'LeftAndMain',
FieldList::create($field)
);

$this->assertEquals(
'admin/LeftAndMain/field/Name',
$field->Link(),
'Link is updated with the key'
);
}
}

0 comments on commit eea80c7

Please sign in to comment.