[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/includes/ -> factory.php (source)

   1  <?php
   2  class BP_UnitTest_Factory extends WP_UnitTest_Factory {
   3      public $activity = null;
   4  
   5  	function __construct() {
   6          parent::__construct();
   7  
   8          $this->user = new BP_UnitTest_Factory_For_User( $this );
   9          $this->activity = new BP_UnitTest_Factory_For_Activity( $this );
  10          $this->group = new BP_UnitTest_Factory_For_Group( $this );
  11          $this->message = new BP_UnitTest_Factory_For_Message( $this );
  12          $this->xprofile_group = new BP_UnitTest_Factory_For_XProfileGroup( $this );
  13          $this->xprofile_field = new BP_UnitTest_Factory_For_XProfileField( $this );
  14          $this->notification = new BP_UnitTest_Factory_For_Notification( $this );
  15          $this->signup = new BP_UnitTest_Factory_For_Signup( $this );
  16          $this->friendship = new BP_UnitTest_Factory_For_Friendship( $this );
  17      }
  18  }
  19  
  20  class BP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_User {
  21      /**
  22       * When creating a new user, it's almost always necessary to have the
  23       * last_activity usermeta set right away, so that the user shows up in
  24       * directory queries. This is a shorthand wrapper for the user factory
  25       * create() method.
  26       *
  27       * Also set a display name
  28       */
  29  	public function create_object( $args ) {
  30          $r = bp_parse_args( $args, array(
  31              'role' => 'subscriber',
  32              'last_activity' => date( 'Y-m-d H:i:s', strtotime( bp_core_current_time() ) - 60*60*24*365 ),
  33          ) );
  34  
  35          $last_activity = $r['last_activity'];
  36          unset( $r['last_activity'] );
  37  
  38          $user_id = wp_insert_user( $r );
  39  
  40          bp_update_user_last_activity( $user_id, $last_activity );
  41  
  42          if ( bp_is_active( 'xprofile' ) ) {
  43              $user = new WP_User( $user_id );
  44              xprofile_set_field_data( 1, $user_id, $user->display_name );
  45          }
  46  
  47          return $user_id;
  48      }
  49  }
  50  
  51  class BP_UnitTest_Factory_For_Activity extends WP_UnitTest_Factory_For_Thing {
  52  
  53  	function __construct( $factory = null ) {
  54          parent::__construct( $factory );
  55  
  56          $this->default_generation_definitions = array(
  57              'component'    => buddypress()->activity->id,
  58              'content'      => new WP_UnitTest_Generator_Sequence( 'Activity content %s' ),
  59              'primary_link' => 'http://example.com',
  60              'type'         => 'activity_update',
  61              'recorded_time' => bp_core_current_time(),
  62          );
  63      }
  64  
  65  	function create_object( $args ) {
  66          if ( ! isset( $args['user_id'] ) )
  67              $args['user_id'] = get_current_user_id();
  68  
  69          return bp_activity_add( $args );
  70      }
  71  
  72  	function update_object( $activity_id, $fields ) {
  73          $activity = new BP_Activity_Activity( $activity_id );
  74  
  75          foreach ( $fields as $field_name => $value ) {
  76              if ( isset( $activity->$field_name ) )
  77                  $activity->$field_name = $value;
  78          }
  79  
  80          $activity->save();
  81          return $activity;
  82      }
  83  
  84  	function get_object_by_id( $user_id ) {
  85          return new BP_Activity_Activity( $user_id );
  86      }
  87  }
  88  
  89  class BP_UnitTest_Factory_For_Group extends WP_UnitTest_Factory_For_Thing {
  90  
  91  	function __construct( $factory = null ) {
  92          parent::__construct( $factory );
  93  
  94          $this->default_generation_definitions = array(
  95              'name'         => new WP_UnitTest_Generator_Sequence( 'Group %s' ),
  96              'description'  => new WP_UnitTest_Generator_Sequence( 'Group description %s' ),
  97              'slug'         => new WP_UnitTest_Generator_Sequence( 'group-slug-%s' ),
  98              'status'       => 'public',
  99              'enable_forum' => true,
 100              'date_created' => bp_core_current_time(),
 101          );
 102      }
 103  
 104  	function create_object( $args ) {
 105          if ( ! isset( $args['creator_id'] ) ) {
 106              if ( is_user_logged_in() ) {
 107                  $args['creator_id'] = get_current_user_id();
 108  
 109              // Create a user. This is based on from BP_UnitTestCase->create_user().
 110              } else {
 111                  $last_activity      = date( 'Y-m-d H:i:s', strtotime( bp_core_current_time() ) - 60 * 60 * 24 * 365 );
 112                  $user_factory       = new WP_UnitTest_Factory_For_User();
 113                  $args['creator_id'] = $this->factory->user->create( array( 'role' => 'subscriber' ) );
 114  
 115                  bp_update_user_last_activity( $args['creator_id'] , $last_activity );
 116  
 117                  if ( bp_is_active( 'xprofile' ) ) {
 118                      $user = new WP_User( $args['creator_id']  );
 119                      xprofile_set_field_data( 1, $args['creator_id'] , $user->display_name );
 120                  }
 121              }
 122          }
 123  
 124          $group_id = groups_create_group( $args );
 125          if ( ! $group_id ) {
 126              return false;
 127          }
 128  
 129          groups_update_groupmeta( $group_id, 'total_member_count', 1 );
 130          $last_activity = isset( $args['last_activity'] ) ? $args['last_activity'] : bp_core_current_time();
 131          groups_update_groupmeta( $group_id, 'last_activity', $last_activity );
 132  
 133          return $group_id;
 134      }
 135  
 136  	function update_object( $group_id, $fields ) {
 137          $group = new BP_Groups_Group( $group_id );
 138  
 139          foreach ( $fields as $field_name => $value ) {
 140              if ( isset( $group->field_name ) )
 141                  $group->field_name = $value;
 142          }
 143  
 144          $group->save();
 145          return $group;
 146      }
 147  
 148  	function get_object_by_id( $group_id ) {
 149          return new BP_Groups_Group( $group_id );
 150      }
 151  }
 152  
 153  class BP_UnitTest_Factory_For_Message extends WP_UnitTest_Factory_For_Thing {
 154  
 155  	function __construct( $factory = null ) {
 156          parent::__construct( $factory );
 157  
 158          $this->default_generation_definitions = array(
 159              'sender_id'  => get_current_user_id(),
 160              'thread_id'  => 0,
 161              'subject'    => new WP_UnitTest_Generator_Sequence( 'Message subject %s' ),
 162              'content'    => new WP_UnitTest_Generator_Sequence( 'Message content %s' ),
 163              'date_sent'  => bp_core_current_time(),
 164          );
 165      }
 166  
 167  	function create_object( $args ) {
 168          if ( empty( $args['sender_id'] ) ) {
 169              $args['sender_id'] = $this->factory->user->create();
 170          }
 171  
 172          if ( empty( $args['recipients'] ) ) {
 173              $recipient = $this->factory->user->create_and_get();
 174              $args['recipients'] = array( $recipient->user_nicename );
 175          }
 176  
 177          $thread_id = messages_new_message( $args );
 178          $thread = new BP_Messages_Thread( $thread_id );
 179          return end( $thread->messages )->id;
 180      }
 181  
 182  	function update_object( $message_id, $fields ) {
 183          // todo
 184      }
 185  
 186  	function get_object_by_id( $message_id ) {
 187          return new BP_Messages_Message( $message_id );
 188      }
 189  }
 190  
 191  class BP_UnitTest_Factory_For_XProfileGroup extends WP_UnitTest_Factory_For_Thing {
 192  
 193  	function __construct( $factory = null ) {
 194          parent::__construct( $factory );
 195  
 196          $this->default_generation_definitions = array(
 197              'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile group %s' ),
 198              'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile group description %s' ),
 199              'slug'         => new WP_UnitTest_Generator_Sequence( 'xprofile-group-slug-%s' ),
 200          );
 201      }
 202  
 203  	function create_object( $args ) {
 204          $group_id = xprofile_insert_field_group( $args );
 205          return $group_id;
 206      }
 207  
 208  	function update_object( $group_id, $fields ) {
 209      }
 210  
 211  	function get_object_by_id( $group_id ) {
 212          return new BP_XProfile_Group( $group_id );
 213      }
 214  }
 215  
 216  class BP_UnitTest_Factory_For_XProfileField extends WP_UnitTest_Factory_For_Thing {
 217  
 218  	function __construct( $factory = null ) {
 219          parent::__construct( $factory );
 220  
 221          $this->default_generation_definitions = array(
 222              'name'        => new WP_UnitTest_Generator_Sequence( 'XProfile field %s' ),
 223              'description' => new WP_UnitTest_Generator_Sequence( 'XProfile field description %s' ),
 224              'type'        => 'textbox',
 225          );
 226      }
 227  
 228  	function create_object( $args ) {
 229          $field_id = xprofile_insert_field( $args );
 230          return $field_id;
 231      }
 232  
 233  	function update_object( $field_id, $fields ) {
 234      }
 235  
 236  	function get_object_by_id( $field_id ) {
 237          return new BP_XProfile_Field( $field_id );
 238      }
 239  }
 240  
 241  class BP_UnitTest_Factory_For_Notification extends WP_UnitTest_Factory_For_Thing {
 242  	public function __construct( $factory = null ) {
 243          parent::__construct( $factory );
 244      }
 245  
 246  	public function create_object( $args ) {
 247          return bp_notifications_add_notification( $args );
 248      }
 249  
 250  	public function update_object( $id, $fields ) {}
 251  
 252  	public function get_object_by_id( $id ) {
 253          return new BP_Notifications_Notification( $id );
 254      }
 255  }
 256  
 257  class BP_UnitTest_Factory_For_Signup extends WP_UnitTest_Factory_For_Thing {
 258  	public function __construct( $factory = null ) {
 259          parent::__construct( $factory );
 260      }
 261  
 262  	public function create_object( $args ) {
 263          return BP_Signup::add( $args );
 264      }
 265  
 266  	public function update_object( $id, $fields ) {}
 267  
 268  	public function get_object_by_id( $id ) {
 269          return new BP_Signup( $id );
 270      }
 271  }
 272  
 273  /**
 274   * Factory for friendships.
 275   *
 276   * @since 2.7.0
 277   */
 278  class BP_UnitTest_Factory_For_Friendship extends WP_UnitTest_Factory_For_Thing {
 279      /**
 280       * Constructor.
 281       *
 282       * @since 2.7.0
 283       *
 284       * @param $factory WP_UnitTest_Factory
 285       */
 286  	public function __construct( $factory = null ) {
 287          parent::__construct( $factory );
 288      }
 289  
 290      /**
 291       * Create friendship object.
 292       *
 293       * @since 2.7.0
 294       *
 295       * @param array $args Array of arguments.
 296       * @return int Friendship ID.
 297       */
 298  	public function create_object( $args ) {
 299          $friendship = new BP_Friends_Friendship();
 300  
 301          foreach ( array( 'initiator_user_id', 'friend_user_id' ) as $arg ) {
 302              if ( isset( $args[ $arg ] ) ) {
 303                  $friendship->$arg = $args[ $arg ];
 304              } else {
 305                  $friendship->$arg = $this->factory->user->create();
 306              }
 307          }
 308  
 309          foreach ( array( 'is_confirmed', 'is_limited', 'date_created' ) as $arg ) {
 310              if ( isset( $args[ $arg ] ) ) {
 311                  $friendship->$arg = $args[ $arg ];
 312              }
 313          }
 314  
 315          $friendship->save();
 316  
 317          return $friendship->id;
 318      }
 319  
 320      /**
 321       * Update a friendship object.
 322       *
 323       * @since 2.7.0
 324       *
 325       * @todo Implement.
 326       *
 327       * @param int   $id     ID of the friendship.
 328       * @param array $fields Fields to update.
 329       */
 330  	public function update_object( $id, $fields ) {}
 331  
 332      /**
 333       * Get a friendship object by its ID.
 334       *
 335       * @since 2.7.0
 336       *
 337       * @param int $id
 338       * @return BP_Friends_Friendship
 339       */
 340  	public function get_object_by_id( $id ) {
 341          return new BP_Friends_Friendship( $id );
 342      }
 343  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1