| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\ContentReview\Tests\Extensions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\ContentReview\Forms\ReviewContentHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Security\Member; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class ContentReviewCMSExtensionTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * Test that ReviewContentForm finds an ID parameter then returns the result of getReviewContentForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * with the passed ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function testReviewContentForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $mock = $this->getMockBuilder(ContentReviewCMSExtension::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             ->setMethods(['getReviewContentForm']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $mock->expects($this->once())->method('getReviewContentForm')->with(123)->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $request = new HTTPRequest('GET', '/', [], ['ID' => 123]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $result = $mock->ReviewContentForm($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertTrue($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @expectedException SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @expectedExceptionMessage Bad record ID #1234 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function testGetReviewContentFormThrowsExceptionWhenPageNotFound() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         (new ContentReviewCMSExtension)->getReviewContentForm(1234); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @expectedException SilverStripe\Control\HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @expectedExceptionMessage It seems you don't have the necessary permissions to review this content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function testGetReviewContentFormThrowsExceptionWhenObjectCannotBeReviewed() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->logOut(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $mock = $this->getMockBuilder(ContentReviewCMSExtension::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->setMethods(['findRecord']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $mock->setOwner(new Controller); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // Return a DataObject without the content review extension applied | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $mock->expects($this->once())->method('findRecord')->with(['ID' => 123])->willReturn(new Member); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $mock->getReviewContentForm(123); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Ensure that savereview() calls the ReviewContentHandler and passes the data to it | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     public function testSaveReviewCallsHandler() | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         $mock = $this->getMockBuilder(ContentReviewCMSExtension::class) | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             ->setMethods(['findRecord', 'getReviewContentHandler']) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             ->getMock(); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         $mock->setOwner(new Controller); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         $mockPage = (object) ['ID' => 123]; | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         $mock->expects($this->once())->method('findRecord')->willReturn($mockPage); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         $mockHandler = $this->getMockBuilder(ReviewContentHandler::class) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             ->setMethods(['submitReview']) | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |             ->getMock(); | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         $mockHandler->expects($this->once()) | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             ->method('submitReview') | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             ->with($mockPage, ['foo']) | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             ->willReturn('Success'); | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         $mock->expects($this->once())->method('getReviewContentHandler')->willReturn($mockHandler); | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         $form = $this->getMockBuilder(Form::class) | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |             ->disableOriginalConstructor() | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             ->getMock(); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         $result = $mock->savereview(['foo'], $form); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         $this->assertSame('Success', $result); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |