| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace FSWebWorks\SilverStripe\UserInvitations\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use FSWebWorks\SilverStripe\UserInvitations\Model\UserInvitation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class UserInvitationTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public static $fixture_file = 'UserInvitationTest.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Tests that an invitation email was sent. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function testSendInvitation() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         /** @var UserInvitation $joe */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $joe = $this->objFromFixture(UserInvitation::class, 'joe'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $sent = $joe->sendInvitation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $keys = array_keys($sent->getTo()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->assertEquals($joe->Email, $keys[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertEquals("Invitation from {$joe->InvitedBy()->FirstName}", $sent->getSubject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertContains('Click here to accept this invitation', $sent->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Tests for expired invitations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function testIsExpired() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         /** @var UserInvitation $expired */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $expired = $this->objFromFixture(UserInvitation::class, 'expired'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertTrue($expired->isExpired()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Tests that the TempHash field has been removed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function testGetCMSFields() | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         /** @var UserInvitation $joe */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $joe = $this->objFromFixture(UserInvitation::class, 'joe'); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $fields = $joe->getCMSFields(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertNull($fields->dataFieldByName('TempHash')); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertNotNull($fields->dataFieldByName('FirstName')); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertNotNull($fields->dataFieldByName('Email')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Tests that invitations can't be re-sent. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function testInvitationAlreadySent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $invite = UserInvitation::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'FirstName' => 'Joe', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'Email' => '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $result = $invite->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertFalse($result->isValid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertEquals('This user was already sent an invite.', $result->getMessages()[0]['message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Tests that duplicate members can't be created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function testMemberAlreadyExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $invite = UserInvitation::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'FirstName' => 'Jane', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'Email' => '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $result = $invite->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->assertFalse($result->isValid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->assertEquals('This person is already a member of this system.', $result->getMessages()[0]['message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Tests that a random hash and the logged in users id was added | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function testOnBeforeWrite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->logInWithPermission('ADMIN'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $invite = UserInvitation::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'FirstName' => 'Dane', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'Email' => '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $invite->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->assertNotNull($invite->TempHash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->assertNotNull($invite->InvitedByID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |