Source of file KapostConversionTest.php
Size: 15,200 Bytes - Last Modified: 2021-12-23T10:57:38+00:00
/var/www/docs.ssmods.com/process/src/tests/KapostConversionTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 | <?php class KapostConversionTest extends FunctionalTest { private static $configured=false; private static $_fixtureFactory; private static $_fixtures; /** * @var KapostTestController */ protected $testController; /** * Adds the extra data objects if the SiteTree class exists */ public function setUpOnce() { if(class_exists('SiteTree')) { $this->extraDataObjects=array( 'KapostConversionTest_KapostTestPage', 'KapostConversionTest_TestPage' ); } parent::setUpOnce(); } /** * Initializes the database, we do it here so we don't loose our information we need a sequential testing environment */ public function setUp() { parent::setUp(); if(!class_exists('SiteTree')) { $this->markTestSkipped('CMS not installed skipping test'); return; } //Remove all extensions (keeps the tests sane) $extensions=Object::get_extensions('KapostGridFieldDetailForm_ItemRequest'); if(!empty($extensions)) { foreach($extensions as $extension) { KapostGridFieldDetailForm_ItemRequest::remove_extension($extension); } } $this->testController=new KapostTestController(); if(self::$configured==false) { $prefix=(defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX:'ss_'); $fixtureFile='KapostConversionTest.yml'; // Set up fixture if($fixtureFile || $this->usesDatabase || !self::using_temp_db()) { if(substr(DB::getConn()->currentDatabase(), 0, strlen($prefix) + 5) != strtolower(sprintf('%stmpdb', $prefix))) { self::create_temp_db(); } singleton('DataObject')->flushCache(); self::empty_temp_db(); foreach($this->requireDefaultRecordsFrom as $className) { $instance = singleton($className); if (method_exists($instance, 'requireDefaultRecords')) $instance->requireDefaultRecords(); if (method_exists($instance, 'augmentDefaultRecords')) $instance->augmentDefaultRecords(); } if($fixtureFile) { $pathForClass = $this->getCurrentAbsolutePath(); $fixtureFiles = (is_array($fixtureFile)) ? $fixtureFile : array($fixtureFile); $i = 0; foreach($fixtureFiles as $fixtureFilePath) { // Support fixture paths relative to the test class, rather than relative to webroot // String checking is faster than file_exists() calls. $isRelativeToFile = (strpos('/', $fixtureFilePath) === false || preg_match('/^\.\./', $fixtureFilePath)); if($isRelativeToFile) { $resolvedPath = realpath($pathForClass . '/' . $fixtureFilePath); if($resolvedPath) $fixtureFilePath = $resolvedPath; } $fixture = Injector::inst()->create('YamlFixture', $fixtureFilePath); $fixture->writeInto($this->getFixtureFactory()); $this->fixtures[] = $fixture; // backwards compatibility: Load first fixture into $this->fixture if($i == 0) $this->fixture = $fixture; $i++; } self::$_fixtureFactory=$this->getFixtureFactory(); self::$_fixtures=$this->fixtures; } $this->logInWithPermission("ADMIN"); } self::$configured=true; }else { $this->fixtureFactory=self::$_fixtureFactory; $this->fixtures=self::$_fixtures; } } /** * Tests conversion of new pages to the actual staged page */ public function testConvertNewPage() { if(!class_exists('SiteTree')) { $this->markTestSkipped('CMS not installed skipping test'); return; } //Ensure we're an admin $this->logInWithPermission('ADMIN'); //Get the kapost object $kapostObj=$this->objFromFixture('KapostPage', 'newpage'); //Build the url of the request $url=Controller::join_links( $this->testController->TestForm()->Fields()->dataFieldByName('TestGrid')->Link('item'), $kapostObj->ID, 'ConvertObjectForm' ); //Send the mock request $testData=array( 'ConvertMode'=>'NewPage', 'ParentPageID'=>0, 'action_doConvertObject'=>'Test' ); $response=$this->post($url, $testData); //Verify a 200 response $this->assertEquals(200, $response->getStatusCode()); //Reset versioned Versioned::reset(); //Check to see the page got converted $page=SiteTree::get()->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Verify the fields are correct $this->assertEquals($kapostObj->Title, $page->Title); $this->assertEquals($kapostObj->Content, $page->Content); $this->assertEquals($kapostObj->MenuTitle, $page->MenuTitle); $this->assertEquals($kapostObj->MetaDescription, $page->MetaDescription); } /** * Tests conversion of edits for a page to the actual staged page */ public function testConvertReplacePage() { if(!class_exists('SiteTree')) { $this->markTestSkipped('CMS not installed skipping test'); return; } //Ensure we're an admin $this->logInWithPermission('ADMIN'); //Get the kapost object $kapostObj=$this->objFromFixture('KapostPage', 'editpage'); //Build the url of the request $url=Controller::join_links( $this->testController->TestForm()->Fields()->dataFieldByName('TestGrid')->Link('item'), $kapostObj->ID, 'ConvertObjectForm' ); $page=SiteTree::get()->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Send the mock request $testData=array( 'ConvertMode'=>'ReplacePage', 'ReplacePageID'=>$page->ID, 'action_doConvertObject'=>'Test' ); $response=$this->post($url, $testData); //Verify a 200 response $this->assertEquals(200, $response->getStatusCode()); //Reset versioned Versioned::reset(); //Check to see the page got converted $page=SiteTree::get()->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Verify the fields are correct $this->assertEquals($kapostObj->Title, $page->Title); $this->assertEquals($kapostObj->Content, $page->Content); $this->assertEquals($kapostObj->MenuTitle, $page->MenuTitle); $this->assertEquals($kapostObj->MetaDescription, $page->MetaDescription); //Make sure it was published per direction $page=Versioned::get_by_stage('SiteTree', 'Live')->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); } /** * Tests conversion of new pages to the actual staged page with a parent */ public function testConvertNewPageParent() { if(!class_exists('SiteTree')) { $this->markTestSkipped('CMS not installed skipping test'); return; } //Ensure we're an admin $this->logInWithPermission('ADMIN'); //Get the kapost object $kapostObj=$this->objFromFixture('KapostPage', 'newpage2'); //Get the parent page $parentPage=$this->objFromFixture('Page', 'parentpage'); //Build the url of the request $url=Controller::join_links( $this->testController->TestForm()->Fields()->dataFieldByName('TestGrid')->Link('item'), $kapostObj->ID, 'ConvertObjectForm' ); //Send the mock request $testData=array( 'ConvertMode'=>'NewPage', 'ParentPageID'=>$parentPage->ID, 'action_doConvertObject'=>'Test' ); $response=$this->post($url, $testData); //Verify a 200 response $this->assertEquals(200, $response->getStatusCode()); //Reset versioned Versioned::reset(); //Check to see the page got converted $page=SiteTree::get()->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Verify the fields are correct $this->assertEquals($parentPage->ID, $page->ParentID); } /** * Tests conversion of edits for a page to the actual staged page */ public function testConvertReplaceCustomPage() { if(!class_exists('SiteTree')) { $this->markTestSkipped('CMS not installed skipping test'); return; } //Ensure we're an admin $this->logInWithPermission('ADMIN'); //Get the test image $image=$this->objFromFixture('Image', 'testimage'); //If the file doesn't exist create it if(!file_exists($image->getFullPath())) { if(!file_exists(dirname($image->getFullPath()))) { mkdir(dirname($image->getFullPath())); } touch($image->getFullPath()); } //Get the kapost object $kapostObj=$this->objFromFixture('KapostConversionTest_KapostTestPage', 'editcustomtypepage'); //Build the url of the request $url=Controller::join_links( $this->testController->TestForm()->Fields()->dataFieldByName('TestGrid')->Link('item'), $kapostObj->ID, 'ConvertObjectForm' ); $page=SiteTree::get()->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Send the mock request $testData=array( 'ConvertMode'=>'ReplacePage', 'ReplacePageID'=>$page->ID, 'action_doConvertObject'=>'Test' ); $response=$this->post($url, $testData); //Verify a 200 response $this->assertEquals(200, $response->getStatusCode()); //Reset versioned Versioned::reset(); //Check to see the page got converted $page=SiteTree::get()->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Verify the fields are correct $this->assertEquals($kapostObj->Title, $page->Title); $this->assertEquals($kapostObj->Content, $page->Content); $this->assertEquals($kapostObj->MenuTitle, $page->MenuTitle); $this->assertEquals($kapostObj->MetaDescription, $page->MetaDescription); //Verify the image was correctly set $this->assertEquals($image->ID, $page->TestImageID); //Make sure it was published per direction $page=Versioned::get_by_stage('SiteTree', 'Live')->filter('KapostRefID', $kapostObj->KapostRefID)->first(); $this->assertNotEmpty($page); $this->assertNotEquals(false, $page); $this->assertTrue($page->exists()); //Remove the test image unlink($image->getFullPath()); } } class KapostTestController extends Controller implements TestOnly { private static $allowed_actions=array( 'TestForm' ); /** * Mock test form used for checking the conversion process */ public function TestForm() { $gridConfig=GridFieldConfig_RecordEditor::create(); $gridConfig->getComponentByType('GridFieldDetailForm')->setItemRequestClass('KapostGridFieldDetailForm_ItemRequest'); $fields=new FieldList( new GridField('TestGrid', 'TestGrid', KapostObject::get(), $gridConfig) ); return new Form($this, 'TestForm', $fields, new FieldList()); } } if(!class_exists('SiteTree')) { return; } class KapostConversionTest_KapostTestPage extends KapostPage implements TestOnly { private static $has_one=array( 'TestImage'=>'Image' ); /** * Gets the destination class when converting to the final object * @return string Class to convert to */ public function getDestinationClass() { return 'KapostConversionTest_TestPage'; } } class KapostConversionTest_TestPage extends Page implements TestOnly { private static $has_one=array( 'TestImage'=>'Image' ); } ?> |