| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group xprofile |
| 5 | * @group activity |
| 6 | */ |
| 7 | class BP_Tests_XProfile_Activity extends BP_UnitTestCase { |
| 8 | protected $updated_profile_data = array(); |
| 9 | |
| 10 | /** |
| 11 | * @group bp_xprofile_updated_profile_activity |
| 12 | */ |
| 13 | public function test_bp_xprofile_updated_profile_activity_with_errors() { |
| 14 | $d = $this->setup_updated_profile_data(); |
| 15 | |
| 16 | // Fake new/old values to ensure a change |
| 17 | $old_values = array( |
| 18 | $this->updated_profile_data['f']->id => array( |
| 19 | 'value' => 'foo', |
| 20 | 'visibility' => 'public', |
| 21 | ), |
| 22 | ); |
| 23 | $new_values = array( |
| 24 | $this->updated_profile_data['f']->id => array( |
| 25 | 'value' => 'foo2', |
| 26 | 'visibility' => 'public', |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), true ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @group bp_xprofile_updated_profile_activity |
| 35 | */ |
| 36 | public function test_bp_xprofile_updated_profile_activity_throttled() { |
| 37 | $d = $this->setup_updated_profile_data(); |
| 38 | |
| 39 | $time = time(); |
| 40 | $prev_time = date( 'Y-m-d h:i:s', $time - ( 119 * 60 ) ); |
| 41 | $now_time = date( 'Y-m-d h:i:s', $time ); |
| 42 | |
| 43 | $this->factory->activity->create( array( |
| 44 | 'user_id' => $d['u'], |
| 45 | 'component' => buddypress()->profile->id, |
| 46 | 'type' => 'updated_profile', |
| 47 | 'date_recorded' => $prev_time, |
| 48 | ) ); |
| 49 | |
| 50 | // Fake new/old values to ensure a change |
| 51 | $old_values = array( |
| 52 | $this->updated_profile_data['f']->id => array( |
| 53 | 'value' => 'foo', |
| 54 | 'visibility' => 'public', |
| 55 | ), |
| 56 | ); |
| 57 | $new_values = array( |
| 58 | $this->updated_profile_data['f']->id => array( |
| 59 | 'value' => 'foo2', |
| 60 | 'visibility' => 'public', |
| 61 | ), |
| 62 | ); |
| 63 | |
| 64 | $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @group bp_xprofile_updated_profile_activity |
| 69 | */ |
| 70 | public function test_bp_xprofile_updated_profile_activity_outside_of_throttle() { |
| 71 | $d = $this->setup_updated_profile_data(); |
| 72 | |
| 73 | $time = time(); |
| 74 | $prev_time = date( 'Y-m-d h:i:s', $time - ( 121 * 60 ) ); |
| 75 | $now_time = date( 'Y-m-d h:i:s', $time ); |
| 76 | |
| 77 | $this->factory->activity->create( array( |
| 78 | 'user_id' => $d['u'], |
| 79 | 'component' => buddypress()->profile->id, |
| 80 | 'type' => 'updated_profile', |
| 81 | 'recorded_time' => $prev_time, |
| 82 | ) ); |
| 83 | |
| 84 | // Fake new/old values to ensure a change |
| 85 | $old_values = array( |
| 86 | $this->updated_profile_data['f']->id => array( |
| 87 | 'value' => 'foo', |
| 88 | 'visibility' => 'public', |
| 89 | ), |
| 90 | ); |
| 91 | $new_values = array( |
| 92 | $this->updated_profile_data['f']->id => array( |
| 93 | 'value' => 'foo2', |
| 94 | 'visibility' => 'public', |
| 95 | ), |
| 96 | ); |
| 97 | |
| 98 | $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 99 | |
| 100 | $existing = bp_activity_get( array( |
| 101 | 'max' => 1, |
| 102 | 'filter' => array( |
| 103 | 'user_id' => $user_id, |
| 104 | 'object' => buddypress()->profile->id, |
| 105 | 'action' => 'updated_profile', |
| 106 | ), |
| 107 | ) ); |
| 108 | |
| 109 | $this->assertEquals( 1, $existing['total'] ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @group bp_xprofile_updated_profile_activity |
| 114 | */ |
| 115 | public function test_bp_xprofile_updated_profile_activity_no_existing_activity() { |
| 116 | $d = $this->setup_updated_profile_data(); |
| 117 | |
| 118 | // Fake new/old values to ensure a change |
| 119 | $old_values = array( |
| 120 | $this->updated_profile_data['f']->id => array( |
| 121 | 'value' => 'foo', |
| 122 | 'visibility' => 'public', |
| 123 | ), |
| 124 | ); |
| 125 | $new_values = array( |
| 126 | $this->updated_profile_data['f']->id => array( |
| 127 | 'value' => 'foo2', |
| 128 | 'visibility' => 'public', |
| 129 | ), |
| 130 | ); |
| 131 | |
| 132 | $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 133 | |
| 134 | $existing = bp_activity_get( array( |
| 135 | 'max' => 1, |
| 136 | 'filter' => array( |
| 137 | 'user_id' => $user_id, |
| 138 | 'object' => buddypress()->profile->id, |
| 139 | 'action' => 'updated_profile', |
| 140 | ), |
| 141 | ) ); |
| 142 | |
| 143 | $this->assertEquals( 1, $existing['total'] ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @group bp_xprofile_updated_profile_activity |
| 148 | */ |
| 149 | public function test_bp_xprofile_updated_profile_activity_no_changes() { |
| 150 | $d = $this->setup_updated_profile_data(); |
| 151 | |
| 152 | $old_values = array( |
| 153 | $this->updated_profile_data['f']->id => array( |
| 154 | 'value' => 'foo', |
| 155 | 'visibility' => 'public', |
| 156 | ), |
| 157 | ); |
| 158 | $new_values = array( |
| 159 | $this->updated_profile_data['f']->id => array( |
| 160 | 'value' => 'foo', |
| 161 | 'visibility' => 'public', |
| 162 | ), |
| 163 | ); |
| 164 | |
| 165 | $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @group bp_xprofile_updated_profile_activity |
| 170 | */ |
| 171 | public function test_bp_xprofile_updated_profile_activity_no_public_changes() { |
| 172 | $d = $this->setup_updated_profile_data(); |
| 173 | |
| 174 | $old_values = array( |
| 175 | $this->updated_profile_data['f']->id => array( |
| 176 | 'value' => 'foo', |
| 177 | 'visibility' => 'loggedin', |
| 178 | ), |
| 179 | ); |
| 180 | $new_values = array( |
| 181 | $this->updated_profile_data['f']->id => array( |
| 182 | 'value' => 'bar', |
| 183 | 'visibility' => 'loggedin', |
| 184 | ), |
| 185 | ); |
| 186 | |
| 187 | $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @group bp_xprofile_updated_profile_activity |
| 192 | */ |
| 193 | public function test_bp_xprofile_updated_profile_activity_public_changed_to_private() { |
| 194 | $d = $this->setup_updated_profile_data(); |
| 195 | |
| 196 | $old_values = array( |
| 197 | $this->updated_profile_data['f']->id => array( |
| 198 | 'value' => 'foo', |
| 199 | 'visibility' => 'public', |
| 200 | ), |
| 201 | ); |
| 202 | $new_values = array( |
| 203 | $this->updated_profile_data['f']->id => array( |
| 204 | 'value' => 'bar', |
| 205 | 'visibility' => 'loggedin', |
| 206 | ), |
| 207 | ); |
| 208 | |
| 209 | $this->assertFalse( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @group bp_xprofile_updated_profile_activity |
| 214 | */ |
| 215 | public function test_bp_xprofile_updated_profile_activity_private_changed_to_public() { |
| 216 | $d = $this->setup_updated_profile_data(); |
| 217 | |
| 218 | $old_values = array( |
| 219 | $this->updated_profile_data['f']->id => array( |
| 220 | 'value' => 'foo', |
| 221 | 'visibility' => 'loggedin', |
| 222 | ), |
| 223 | ); |
| 224 | $new_values = array( |
| 225 | $this->updated_profile_data['f']->id => array( |
| 226 | 'value' => 'foo', |
| 227 | 'visibility' => 'public', |
| 228 | ), |
| 229 | ); |
| 230 | |
| 231 | $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @group bp_xprofile_updated_profile_activity |
| 236 | */ |
| 237 | public function test_bp_xprofile_updated_profile_activity_field_didnt_previously_exist() { |
| 238 | $d = $this->setup_updated_profile_data(); |
| 239 | |
| 240 | $old_values = array(); |
| 241 | $new_values = array( |
| 242 | $this->updated_profile_data['f']->id => array( |
| 243 | 'value' => 'bar', |
| 244 | 'visibility' => 'public', |
| 245 | ), |
| 246 | ); |
| 247 | |
| 248 | $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @group bp_xprofile_updated_profile_activity |
| 253 | */ |
| 254 | public function test_bp_xprofile_updated_profile_activity_public_changes() { |
| 255 | $d = $this->setup_updated_profile_data(); |
| 256 | |
| 257 | $old_values = array( |
| 258 | $this->updated_profile_data['f']->id => array( |
| 259 | 'value' => 'foo', |
| 260 | 'visibility' => 'public', |
| 261 | ), |
| 262 | ); |
| 263 | $new_values = array( |
| 264 | $this->updated_profile_data['f']->id => array( |
| 265 | 'value' => 'bar', |
| 266 | 'visibility' => 'public', |
| 267 | ), |
| 268 | ); |
| 269 | |
| 270 | $this->assertTrue( bp_xprofile_updated_profile_activity( $d['u'], array( $d['f']->id ), false, $old_values, $new_values ) ); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | protected function setup_updated_profile_data() { |
| 275 | $this->updated_profile_data['u'] = $this->create_user(); |
| 276 | $this->updated_profile_data['g'] = $this->factory->xprofile_group->create(); |
| 277 | $this->updated_profile_data['f'] = $this->factory->xprofile_field->create( array( |
| 278 | 'type' => 'textbox', |
| 279 | 'field_group_id' => $this->updated_profile_data['g']->id, |
| 280 | ) ); |
| 281 | |
| 282 | } |
| 283 | } |