public function testSendInvite_PropertiesFromToBodyOk()
 {
     $e = new MongoTestEnvironment();
     $e->clean();
     $inviterUserId = $e->createUser('inviterUser', 'Inviter User', '*****@*****.**');
     $inviterUser = new UserModel($inviterUserId);
     $toUserId = $e->createUser('touser', 'To Name', '');
     $toUser = new UserModel($toUserId);
     $toUser->emailPending = '*****@*****.**';
     $toUser->write();
     $project = $e->createProjectSettings(SF_TESTPROJECTCODE);
     $delivery = new MockCommunicateDelivery();
     Communicate::sendInvite($inviterUser, $toUser, $project, $e->website, $delivery);
     // What's in the delivery?
     $senderEmail = 'no-reply@' . $e->website->domain;
     $expectedFrom = array($senderEmail => $e->website->name);
     $expectedTo = array($toUser->emailPending => $toUser->name);
     $this->assertEqual($expectedFrom, $delivery->from);
     $this->assertEqual($expectedTo, $delivery->to);
     $this->assertPattern('/account signup validation/', $delivery->subject);
     $this->assertPattern('/Inviter User/', $delivery->content);
     $this->assertPattern('/' . $e->website->domain . '\\/registration/', $delivery->content);
 }
 /**
  * Sends an email to invite emailee to join the project
  * @param string $projectId
  * @param string $inviterUserId
  * @param Website $website
  * @param string $toEmail
  * @param DeliveryInterface $delivery
  * @throws \Exception
  * @return string $userId
  */
 public static function sendInvite($projectId, $inviterUserId, $website, $toEmail, DeliveryInterface $delivery = null)
 {
     $newUser = new UserModel();
     $inviterUser = new UserModel($inviterUserId);
     $project = new ProjectModel($projectId);
     $newUser->emailPending = $toEmail;
     // Check if email already exists in an account
     $identityCheck = UserCommands::checkIdentity('', $toEmail, $website);
     if ($identityCheck->emailExists) {
         $newUser->readByProperty('email', $toEmail);
     }
     // Make sure the user exists on the site
     if (!$newUser->hasRoleOnSite($website)) {
         $newUser->siteRole[$website->domain] = $website->userDefaultSiteRole;
     }
     // Determine if user is already a member of the project
     if ($project->userIsMember($newUser->id->asString())) {
         return $newUser->id;
     }
     // Add the user to the project
     $newUser->addProject($project->id->asString());
     $userId = $newUser->write();
     $project->addUser($userId, ProjectRoles::CONTRIBUTOR);
     $project->write();
     if (!$identityCheck->emailExists) {
         // Email communication with new user
         Communicate::sendInvite($inviterUser, $newUser, $project, $website, $delivery);
     } else {
         // Tell existing user they're now part of the project
         Communicate::sendAddedToProject($inviterUser, $newUser, $project, $website, $delivery);
     }
     return $userId;
 }