3 namespace RedUNIT\Blackhole;
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
11 * This test suite tests whether beans are marked as tainted
12 * under the correct circumstances.
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
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.
23 class Tainted extends Blackhole
26 * Test whether we can detect a change using hasChanged().
30 public function testHasChangedList()
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 );
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 );
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 );
74 * Tests whether we can clear the history of a bean.
78 public function testClearHist()
81 $book = R::dispense( 'book' );
82 asrt( $book->hasChanged( 'title' ), FALSE );
83 $book->title = 'book';
84 asrt( $book->hasChanged( 'title' ), TRUE );
86 asrt( $book->hasChanged( 'title' ), TRUE );
87 $book->clearHistory();
88 asrt( $book->hasChanged( 'title' ), FALSE );
96 public function testTainted()
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' );
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
113 asrt( $note->isTainted(), FALSE );
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 );
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' );