| 936 | |
| 937 | /** |
| 938 | * @dataProvider _dp_rest_filter_response_by_context |
| 939 | */ |
| 940 | public function test_rest_filter_response_by_context( $schema, $data, $expected ) { |
| 941 | $this->assertEquals( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) ); |
| 942 | } |
| 943 | |
| 944 | public function _dp_rest_filter_response_by_context() { |
| 945 | return array( |
| 946 | 'default' => array( |
| 947 | array( |
| 948 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 949 | 'type' => 'object', |
| 950 | 'properties' => array( |
| 951 | 'first' => array( |
| 952 | 'type' => 'string', |
| 953 | 'context' => array( 'view', 'edit' ), |
| 954 | ), |
| 955 | 'second' => array( |
| 956 | 'type' => 'string', |
| 957 | 'context' => array( 'edit' ), |
| 958 | ), |
| 959 | ), |
| 960 | ), |
| 961 | array( |
| 962 | 'first' => 'a', |
| 963 | 'second' => 'b', |
| 964 | ), |
| 965 | array( 'first' => 'a' ), |
| 966 | ), |
| 967 | 'keeps missing context' => array( |
| 968 | array( |
| 969 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 970 | 'type' => 'object', |
| 971 | 'properties' => array( |
| 972 | 'first' => array( |
| 973 | 'type' => 'string', |
| 974 | 'context' => array( 'view', 'edit' ), |
| 975 | ), |
| 976 | 'second' => array( |
| 977 | 'type' => 'string', |
| 978 | ), |
| 979 | ), |
| 980 | ), |
| 981 | array( |
| 982 | 'first' => 'a', |
| 983 | 'second' => 'b', |
| 984 | ), |
| 985 | array( |
| 986 | 'first' => 'a', |
| 987 | 'second' => 'b', |
| 988 | ), |
| 989 | ), |
| 990 | 'removes empty context' => array( |
| 991 | array( |
| 992 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 993 | 'type' => 'object', |
| 994 | 'properties' => array( |
| 995 | 'first' => array( |
| 996 | 'type' => 'string', |
| 997 | 'context' => array( 'view', 'edit' ), |
| 998 | ), |
| 999 | 'second' => array( |
| 1000 | 'type' => 'string', |
| 1001 | 'context' => array(), |
| 1002 | ), |
| 1003 | ), |
| 1004 | ), |
| 1005 | array( |
| 1006 | 'first' => 'a', |
| 1007 | 'second' => 'b', |
| 1008 | ), |
| 1009 | array( 'first' => 'a' ), |
| 1010 | ), |
| 1011 | 'nested properties' => array( |
| 1012 | array( |
| 1013 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1014 | 'type' => 'object', |
| 1015 | 'properties' => array( |
| 1016 | 'parent' => array( |
| 1017 | 'type' => 'object', |
| 1018 | 'context' => array( 'view', 'edit' ), |
| 1019 | 'properties' => array( |
| 1020 | 'child' => array( |
| 1021 | 'type' => 'string', |
| 1022 | 'context' => array( 'view', 'edit' ), |
| 1023 | ), |
| 1024 | 'hidden' => array( |
| 1025 | 'type' => 'string', |
| 1026 | 'context' => array( 'edit' ), |
| 1027 | ), |
| 1028 | ), |
| 1029 | ), |
| 1030 | ), |
| 1031 | ), |
| 1032 | array( |
| 1033 | 'parent' => array( |
| 1034 | 'child' => 'hi', |
| 1035 | 'hidden' => 'there', |
| 1036 | ), |
| 1037 | ), |
| 1038 | array( 'parent' => array( 'child' => 'hi' ) ), |
| 1039 | ), |
| 1040 | 'grand child properties' => array( |
| 1041 | array( |
| 1042 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1043 | 'type' => 'object', |
| 1044 | 'properties' => array( |
| 1045 | 'parent' => array( |
| 1046 | 'type' => 'object', |
| 1047 | 'context' => array( 'view', 'edit' ), |
| 1048 | 'properties' => array( |
| 1049 | 'child' => array( |
| 1050 | 'type' => 'object', |
| 1051 | 'context' => array( 'view', 'edit' ), |
| 1052 | 'properties' => array( |
| 1053 | 'grand' => array( |
| 1054 | 'type' => 'string', |
| 1055 | 'context' => array( 'view', 'edit' ), |
| 1056 | ), |
| 1057 | 'hidden' => array( |
| 1058 | 'type' => 'string', |
| 1059 | 'context' => array( 'edit' ), |
| 1060 | ), |
| 1061 | ), |
| 1062 | ), |
| 1063 | ), |
| 1064 | ), |
| 1065 | ), |
| 1066 | ), |
| 1067 | array( |
| 1068 | 'parent' => array( |
| 1069 | 'child' => array( |
| 1070 | 'grand' => 'hi', |
| 1071 | ), |
| 1072 | ), |
| 1073 | ), |
| 1074 | array( 'parent' => array( 'child' => array( 'grand' => 'hi' ) ) ), |
| 1075 | ), |
| 1076 | 'array' => array( |
| 1077 | array( |
| 1078 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1079 | 'type' => 'object', |
| 1080 | 'properties' => array( |
| 1081 | 'arr' => array( |
| 1082 | 'type' => 'array', |
| 1083 | 'context' => array( 'view', 'edit' ), |
| 1084 | 'items' => array( |
| 1085 | 'type' => 'object', |
| 1086 | 'context' => array( 'view', 'edit' ), |
| 1087 | 'properties' => array( |
| 1088 | 'visible' => array( |
| 1089 | 'type' => 'string', |
| 1090 | 'context' => array( 'view', 'edit' ), |
| 1091 | ), |
| 1092 | 'hidden' => array( |
| 1093 | 'type' => 'string', |
| 1094 | 'context' => array( 'edit' ), |
| 1095 | ), |
| 1096 | ), |
| 1097 | ), |
| 1098 | ), |
| 1099 | ), |
| 1100 | ), |
| 1101 | array( |
| 1102 | 'arr' => array( |
| 1103 | array( |
| 1104 | 'visible' => 'hi', |
| 1105 | 'hidden' => 'there', |
| 1106 | ), |
| 1107 | ), |
| 1108 | ), |
| 1109 | array( 'arr' => array( array( 'visible' => 'hi' ) ) ), |
| 1110 | ), |
| 1111 | 'additional properties' => array( |
| 1112 | array( |
| 1113 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1114 | 'type' => 'object', |
| 1115 | 'properties' => array( |
| 1116 | 'additional' => array( |
| 1117 | 'type' => 'object', |
| 1118 | 'context' => array( 'view', 'edit' ), |
| 1119 | 'properties' => array( |
| 1120 | 'a' => array( |
| 1121 | 'type' => 'string', |
| 1122 | 'context' => array( 'view', 'edit' ), |
| 1123 | ), |
| 1124 | 'b' => array( |
| 1125 | 'type' => 'string', |
| 1126 | 'context' => array( 'edit' ), |
| 1127 | ), |
| 1128 | ), |
| 1129 | 'additionalProperties' => array( |
| 1130 | 'type' => 'string', |
| 1131 | 'context' => array( 'edit' ), |
| 1132 | ), |
| 1133 | ), |
| 1134 | ), |
| 1135 | ), |
| 1136 | array( |
| 1137 | 'additional' => array( |
| 1138 | 'a' => '1', |
| 1139 | 'b' => '2', |
| 1140 | 'c' => '3', |
| 1141 | ), |
| 1142 | ), |
| 1143 | array( 'additional' => array( 'a' => '1' ) ), |
| 1144 | ), |
| 1145 | 'multiple types object' => array( |
| 1146 | array( |
| 1147 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1148 | 'type' => 'object', |
| 1149 | 'properties' => array( |
| 1150 | 'multi' => array( |
| 1151 | 'type' => array( 'object', 'string' ), |
| 1152 | 'context' => array( 'view', 'edit' ), |
| 1153 | 'properties' => array( |
| 1154 | 'a' => array( |
| 1155 | 'type' => 'string', |
| 1156 | 'context' => array( 'view', 'edit' ), |
| 1157 | ), |
| 1158 | 'b' => array( |
| 1159 | 'type' => 'string', |
| 1160 | 'context' => array( 'edit' ), |
| 1161 | ), |
| 1162 | ), |
| 1163 | ), |
| 1164 | ), |
| 1165 | ), |
| 1166 | array( |
| 1167 | 'multi' => array( |
| 1168 | 'a' => '1', |
| 1169 | 'b' => '2', |
| 1170 | ), |
| 1171 | ), |
| 1172 | array( 'multi' => array( 'a' => '1' ) ), |
| 1173 | ), |
| 1174 | 'multiple types array' => array( |
| 1175 | array( |
| 1176 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1177 | 'type' => 'object', |
| 1178 | 'properties' => array( |
| 1179 | 'multi' => array( |
| 1180 | 'type' => array( 'array', 'string' ), |
| 1181 | 'context' => array( 'view', 'edit' ), |
| 1182 | 'items' => array( |
| 1183 | 'type' => 'object', |
| 1184 | 'context' => array( 'view', 'edit' ), |
| 1185 | 'properties' => array( |
| 1186 | 'visible' => array( |
| 1187 | 'type' => 'string', |
| 1188 | 'context' => array( 'view', 'edit' ), |
| 1189 | ), |
| 1190 | 'hidden' => array( |
| 1191 | 'type' => 'string', |
| 1192 | 'context' => array( 'edit' ), |
| 1193 | ), |
| 1194 | ), |
| 1195 | ), |
| 1196 | ), |
| 1197 | ), |
| 1198 | ), |
| 1199 | array( |
| 1200 | 'multi' => array( |
| 1201 | array( |
| 1202 | 'visible' => '1', |
| 1203 | 'hidden' => '2', |
| 1204 | ), |
| 1205 | ), |
| 1206 | ), |
| 1207 | array( 'multi' => array( array( 'visible' => '1' ) ) ), |
| 1208 | ), |
| 1209 | 'grand child properties does not traverses missing context' => array( |
| 1210 | array( |
| 1211 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 1212 | 'type' => 'object', |
| 1213 | 'properties' => array( |
| 1214 | 'parent' => array( |
| 1215 | 'type' => 'object', |
| 1216 | 'context' => array( 'view', 'edit' ), |
| 1217 | 'properties' => array( |
| 1218 | 'child' => array( |
| 1219 | 'type' => 'object', |
| 1220 | 'properties' => array( |
| 1221 | 'grand' => array( |
| 1222 | 'type' => 'string', |
| 1223 | 'context' => array( 'view', 'edit' ), |
| 1224 | ), |
| 1225 | 'hidden' => array( |
| 1226 | 'type' => 'string', |
| 1227 | 'context' => array( 'edit' ), |
| 1228 | ), |
| 1229 | ), |
| 1230 | ), |
| 1231 | ), |
| 1232 | ), |
| 1233 | ), |
| 1234 | ), |
| 1235 | array( |
| 1236 | 'parent' => array( |
| 1237 | 'child' => array( |
| 1238 | 'grand' => 'hi', |
| 1239 | 'hidden' => 'there', |
| 1240 | ), |
| 1241 | ), |
| 1242 | ), |
| 1243 | array( |
| 1244 | 'parent' => array( |
| 1245 | 'child' => array( |
| 1246 | 'grand' => 'hi', |
| 1247 | 'hidden' => 'there', |
| 1248 | ), |
| 1249 | ), |
| 1250 | ), |
| 1251 | ), |
| 1252 | ); |
| 1253 | } |