| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SMW\ApprovedRevs\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SMW\ApprovedRevs\DatabaseLogReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SMW\ApprovedRevs\ServicesFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @covers \SMW\ApprovedRevs\DatabaseLogReader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @group semantic-approved-revs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @since 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author mwjames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class DatabaseLogReaderTest extends \PHPUnit_Framework_TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	private $servicesFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	private $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	protected function setUp() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$this->servicesFactory = new ServicesFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->connection = $this->getMockBuilder( '\DatabaseBase' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	public function testCanConstruct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		$this->assertInstanceOf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			DatabaseLogReader::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$this->servicesFactory->newDatabaseLogReader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 	public function testGetQuery() { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 		$log = $this->servicesFactory->newDatabaseLogReader(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		$this->assertNull( | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 			$log->getQuery() | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function testNoLog() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$log = $this->servicesFactory->newDatabaseLogReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$this->assertNull( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			$log->getUserForLogEntry() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function testGetNull() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$title = \Title::newFromText( "none" ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$log = $this->servicesFactory->newDatabaseLogReader( $title ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->assertNull( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			$log->getUserForLogEntry() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$this->assertNull( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$log->getDateOfLogEntry() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->assertNull( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$log->getStatusOfLogEntry() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$query = $log->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			[ 'tables', 'fields', 'conds', 'options', 'join_conds' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			array_keys( $query ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	public function testGetLogAndQuery() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$title = \Title::newFromText( __METHOD__ ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$row = new \stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$row->user_id = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$row->log_timestamp = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$row->log_action = 'bloop'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$this->connection->expects( $this->any() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			->method( 'select' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			->will( $this->returnValue( new \ArrayIterator( [ $row ] ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$this->servicesFactory->setConnection( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$this->connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$log = $this->servicesFactory->newDatabaseLogReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			\User::newFromID( 1 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			$log->getUserForLogEntry( $title ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			new \MWTimestamp( 5 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$log->getDateOfLogEntry( $title ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			'bloop', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			$log->getStatusOfLogEntry( $title ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		$query = $log->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			[ 'tables', 'fields', 'conds', 'options', 'join_conds' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			array_keys( $query ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	public function testCache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$title = \Title::newFromText( __METHOD__ ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		$row = new \stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		$row->user_id = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$row->log_timestamp = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$row->log_action = 'bloop'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$this->connection->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			->method( 'select' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			->will( $this->returnValue( new \ArrayIterator( [ $row ] ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$this->servicesFactory->setConnection( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			$this->connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$log = $this->servicesFactory->newDatabaseLogReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		$log->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			\User::newFromID( 1 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			$log->getUserForLogEntry( $title ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		// Second call on same title instance should be made from cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			\User::newFromID( 1 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			$log->getUserForLogEntry( $title ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |  |