| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\VersionedAdmin\Tests\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Admin\LeftAndMainFormRequestHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\VersionedAdmin\Controllers\HistoryViewerController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\UnviewableVersionedObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\ViewableVersionedObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class HistoryViewerControllerTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected static $fixture_file = 'HistoryViewerControllerTest.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected static $extra_dataobjects = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         ViewableVersionedObject::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         UnviewableVersionedObject::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function testGetClientConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $clientConfig = $controller->getClientConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertArrayHasKey('versionForm', $clientConfig['form']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->assertArrayHasKey('schemaUrl', $clientConfig['form']['versionForm']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function testSchema() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $controllerMock = $this->getMockBuilder(HistoryViewerController::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ->setMethods(['getVersionForm', 'getSchemaResponse']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $controllerMock->expects($this->once())->method('getVersionForm')->with([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'RecordClass' => 'Page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'RecordID' => 123, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'RecordVersion' => 234, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $controllerMock->expects($this->once())->method('getSchemaResponse')->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $request = $this->getMockBuilder(HTTPRequest::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->setConstructorArgs(['GET', '/']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->setMethods(['param']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $request->expects($this->once())->method('param')->with('FormName')->willReturn('versionForm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $request->offsetSet('RecordClass', 'Page'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $request->offsetSet('RecordID', 123); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $request->offsetSet('RecordVersion', 234); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $controllerMock->schema($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         /** @var HTTPResponse $result */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $result = $controllerMock->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->assertSame('application/json', $result->getHeader('Content-Type')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @expectedException InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @expectedExceptionMessage Missing RecordID / RecordVersion / RecordClass for this form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function testGetVersionFormThrowsExceptionWhenArgsAreMissing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $controller->getVersionForm([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @expectedException \SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @expectedExceptionCode 404 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function testGetVersionFormThrowsExceptionWhenArgsAreFalsy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $controller->getVersionForm([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'RecordClass' => 'Page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'RecordID' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'RecordVersion' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @expectedException \SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @expectedExceptionCode 404 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function testGetVersionFormThrowsExceptionWhenRecordVersionDoesntExist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $controller->getVersionForm([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'RecordClass' => UnviewableVersionedObject::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'RecordID' => 99999, // doesn't exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'RecordVersion' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @expectedException \SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @expectedExceptionCode 403 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @expectedExceptionMessage You don't have the necessary permissions to view Unviewable Versioned Object | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     public function testGetVersionFormThrowsExceptionWhenCanViewIsFalse() | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         $controller->getVersionForm([ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             'RecordClass' => UnviewableVersionedObject::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             'RecordID' => $this->idFromFixture(UnviewableVersionedObject::class, 'record_one'), | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             'RecordVersion' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function testGetVersionFormReturnsForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $result = $controller->getVersionForm([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             'RecordClass' => UnviewableVersionedObject::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             'RecordID' => $this->idFromFixture(ViewableVersionedObject::class, 'record_one'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             'RecordVersion' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->assertInstanceOf(Form::class, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->assertInstanceOf(LeftAndMainFormRequestHandler::class, $result->getRequestHandler()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @expectedException \SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @expectedExceptionCode 400 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     public function testVersionFormThrowsExceptionWithoutRequest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $controller->versionForm(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @expectedException \SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @expectedExceptionCode 404 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function testVersionFormThrowsExceptionWhenArgsAreFalsy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $controller = new HistoryViewerController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $controller->getVersionForm([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             'RecordClass' => 'Page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             'RecordID' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             'RecordVersion' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     public function testVersionFormReturnsVersionForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $controllerMock = $this->getMockBuilder(HistoryViewerController::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             ->setMethods(['getVersionForm']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $mockData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             'RecordClass' => 'Page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             'RecordID' => 123, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             'RecordVersion' => 234, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $controllerMock->expects($this->once())->method('getVersionForm') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             ->with($mockData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             ->willReturn('mocked'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $result = $controllerMock->versionForm(new HTTPRequest('GET', '/', $mockData)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $this->assertSame('mocked', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  |