Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Blackhole / Tainted.php
1 <?php
2
3 namespace RedUNIT\Blackhole;
4
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7
8 /**
9  * Tainted
10  *
11  * This test suite tests whether beans are marked as tainted
12  * under the correct circumstances.
13  *
14  * @file    RedUNIT/Blackhole/Tainted.php
15  * @desc    Tests tainted flag for OODBBean objects.
16  * @author  Gabor de Mooij and the RedBeanPHP Community
17  * @license New BSD/GPLv2
18  *
19  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
20  * This source file is subject to the New BSD/GPLv2 License that is bundled
21  * with this source code in the file license.txt.
22  */
23 class Tainted extends Blackhole
24 {
25         /**
26          * Test whether we can detect a change using hasChanged().
27          *
28          * @return void
29          */
30         public function testHasChangedList()
31         {
32                 R::nuke();
33                 $book = R::dispense( 'book' );
34                 $page = R::dispense( 'page' );
35                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
36                 $book->ownPage[] = $page;
37                 asrt( $book->hasListChanged( 'ownPage' ), TRUE );
38                 R::store( $book );
39                 $book = $book->fresh();
40                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
41                 $page = R::dispense( 'page' );
42                 $book->ownPageList[] = $page;
43                 asrt( $book->hasListChanged( 'ownPage' ), TRUE );
44                 R::store( $book );
45                 $book = $book->fresh();
46                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
47                 asrt( count( $book->ownPageList ), 2 );
48                 array_pop( $book->ownPageList );
49                 asrt( count( $book->ownPageList ), 1 );
50                 asrt( $book->hasListChanged( 'ownPage' ), TRUE );
51                 array_pop( $book->ownPageList );
52                 asrt( count( $book->ownPageList ), 0 );
53                 asrt( $book->hasListChanged( 'ownPage' ), TRUE );
54                 $book = $book->fresh();
55                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
56                 asrt( count( $book->ownPageList ), 2 );
57                 $otherPage = R::dispense( 'page' );
58                 array_pop( $book->ownPageList );
59                 $book->ownPageList[] = $otherPage;
60                 asrt( count( $book->ownPageList ), 2 );
61                 asrt( $book->hasListChanged( 'ownPage' ), TRUE );
62                 $book = $book->fresh();
63                 $firstPage = reset( $book->ownPageList );
64                 $firstPage->content = 'abc';
65                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
66                 $book = $book->fresh();
67                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
68                 $lastPage = end( $book->ownPageList );
69                 $lastPage->ownText[] = R::dispense( 'text' );
70                 asrt( $book->hasListChanged( 'ownPage' ), FALSE );
71         }
72
73         /**
74          * Tests whether we can clear the history of a bean.
75          *
76          * @return void
77          */
78         public function testClearHist()
79         {
80                 R::nuke();
81                 $book = R::dispense( 'book' );
82                 asrt( $book->hasChanged( 'title' ), FALSE );
83                 $book->title = 'book';
84                 asrt( $book->hasChanged( 'title' ), TRUE );
85                 R::store( $book );
86                 asrt( $book->hasChanged( 'title' ), TRUE );
87                 $book->clearHistory();
88                 asrt( $book->hasChanged( 'title' ), FALSE );
89         }
90
91         /**
92          * Test tainted.
93          *
94          * @return void
95          */
96         public function testTainted()
97         {
98                 testpack( 'Original Tainted Tests' );
99                 $redbean = R::getRedBean();
100                 $spoon = $redbean->dispense( "spoon" );
101                 asrt( $spoon->getMeta( "tainted" ), TRUE );
102                 $spoon->dirty = "yes";
103                 asrt( $spoon->getMeta( "tainted" ), TRUE );
104                 testpack( 'Tainted List test' );
105                 $note = R::dispense( 'note' );
106                 $note->text = 'abc';
107                 $note->ownNote[] = R::dispense( 'note' )->setAttr( 'text', 'def' );
108                 $id = R::store( $note );
109                 $note = R::load( 'note', $id );
110                 asrt( $note->isTainted(), FALSE );
111                 // Shouldn't affect tainted
112                 $note->text;
113                 asrt( $note->isTainted(), FALSE );
114                 $note->ownNote;
115                 asrt( $note->isTainted(), TRUE );
116                 testpack( 'Tainted Test Old Value' );
117                 $text = $note->old( 'text' );
118                 asrt( $text, 'abc' );
119                 asrt( $note->hasChanged( 'text' ), FALSE );
120                 $note->text = 'xxx';
121                 asrt( $note->hasChanged( 'text' ), TRUE );
122                 $text = $note->old( 'text' );
123                 asrt( $text, 'abc' );
124                 testpack( 'Tainted Non-exist' );
125                 asrt( $note->hasChanged( 'text2' ), FALSE );
126                 testpack( 'Misc Tainted Tests' );
127                 $bean = R::dispense( 'bean' );
128                 $bean->hasChanged( 'prop' );
129                 $bean->old( 'prop' );
130         }
131 }