14
14
*/
15
15
namespace Cake \Test \TestCase \ORM ;
16
16
17
+ use Cake \ORM \Entity ;
17
18
use Cake \ORM \Marshaller ;
18
19
use Cake \ORM \Table ;
19
20
use Cake \ORM \TableRegistry ;
20
21
use Cake \TestSuite \TestCase ;
21
22
23
+
24
+ /**
25
+ * Test entity for mass assignment.
26
+ */
27
+ class OpenEntity extends Entity {
28
+ protected $ _accessible = [
29
+ '* ' => true ,
30
+ ];
31
+ }
32
+
33
+ /**
34
+ * Test entity for mass assignment.
35
+ */
36
+ class ProtectedArticle extends Entity {
37
+ protected $ _accessible = [
38
+ 'title ' => true ,
39
+ 'body ' => true
40
+ ];
41
+ }
42
+
22
43
/**
23
44
* Marshaller test case
24
45
*/
@@ -38,9 +59,14 @@ public function setUp() {
38
59
$ articles ->hasMany ('Comments ' );
39
60
40
61
$ comments = TableRegistry::get ('Comments ' );
62
+ $ users = TableRegistry::get ('Users ' );
41
63
$ comments ->belongsTo ('Articles ' );
42
64
$ comments ->belongsTo ('Users ' );
43
65
66
+ $ articles ->entityClass (__NAMESPACE__ . '\OpenEntity ' );
67
+ $ comments ->entityClass (__NAMESPACE__ . '\OpenEntity ' );
68
+ $ users ->entityClass (__NAMESPACE__ . '\OpenEntity ' );
69
+
44
70
$ this ->articles = $ articles ;
45
71
$ this ->comments = $ comments ;
46
72
}
@@ -77,6 +103,27 @@ public function testOneSimple() {
77
103
$ this ->assertNull ($ result ->isNew (), 'Should be detached ' );
78
104
}
79
105
106
+ /**
107
+ * Test one() follows mass-assignment rules.
108
+ *
109
+ * @return void
110
+ */
111
+ public function testOneAccessibleProperties () {
112
+ $ data = [
113
+ 'title ' => 'My title ' ,
114
+ 'body ' => 'My content ' ,
115
+ 'author_id ' => 1 ,
116
+ 'not_in_schema ' => true
117
+ ];
118
+ $ this ->articles ->entityClass (__NAMESPACE__ . '\ProtectedArticle ' );
119
+ $ marshall = new Marshaller ($ this ->articles );
120
+ $ result = $ marshall ->one ($ data , []);
121
+
122
+ $ this ->assertInstanceOf (__NAMESPACE__ . '\ProtectedArticle ' , $ result );
123
+ $ this ->assertNull ($ result ->author_id );
124
+ $ this ->assertNull ($ result ->not_in_schema );
125
+ }
126
+
80
127
/**
81
128
* test one() with a wrapping model name.
82
129
*
0 commit comments