public function itCleansUserFromRecipientsWhenUserCantReadAtLeastOneChangedField()
 {
     $field1 = new MockTracker_FormElement_Field_Date();
     $value1_previous = new MockTracker_Artifact_ChangesetValue_Date();
     $value1_current = new MockTracker_Artifact_ChangesetValue_Date();
     $dao = new MockTracker_Artifact_Changeset_ValueDao();
     $dar = new MockDataAccessResult();
     $fact = new MockTracker_FormElementFactory();
     $artifact = new MockTracker_Artifact();
     $previous_changeset = new MockTracker_Artifact_Changeset();
     $um = new MockUserManager();
     $comment = new MockTracker_Artifact_Changeset_Comment();
     $current_changeset = new Tracker_Artifact_ChangesetTestVersion();
     $previous_changeset->setReturnValue('getId', 65);
     $previous_changeset->setReturnReference('getValue', $value1_previous, array($field1));
     $previous_changeset->setReturnReference('getUserManager', $um);
     $artifact->setReturnReference('getPreviousChangeset', $previous_changeset, array(66));
     $dar->setReturnValueAt(0, 'current', array('changeset_id' => 66, 'field_id' => 1, 'id' => 11, 'has_changed' => 1));
     $dar->setReturnValue('valid', true);
     $dar->setReturnValueAt(2, 'valid', false);
     $dao->setReturnReference('searchById', $dar);
     $fact->setReturnReference('getFieldById', $field1, array(1));
     $field1->setReturnValue('getId', 1);
     $field1->setReturnValue('getLabel', 'field1');
     $field1->setReturnValue('userCanRead', false);
     $field1->setReturnReference('getChangesetValue', $value1_current, array('*', 11, 1));
     $value1_previous->expectNever('hasChanged');
     $value1_current->setReturnValue('hasChanged', true);
     $value1_current->setReturnValue('diff', 'has changed', array($value1_previous, '*'));
     $comment->setReturnValue('hasEmptyBody', true);
     $current_changeset->setReturnValue('getId', 66);
     $current_changeset->setReturnReference('getValueDao', $dao);
     $current_changeset->setReturnReference('getFormElementFactory', $fact);
     $current_changeset->setReturnReference('getArtifact', $artifact);
     $current_changeset->setReturnReference('getUserManager', $um);
     $current_changeset->setReturnReference('getComment', $comment);
     $recipients = array("recipient1" => true, "recipient2" => true, "recipient3" => true);
     $user1 = stub('PFUser')->getUserName()->returns('recipient1');
     $user2 = stub('PFUser')->getUserName()->returns('recipient2');
     $user3 = stub('PFUser')->getUserName()->returns('recipient3');
     $um->setReturnReference('getUserByUserName', $user1);
     $um->setReturnReference('getUserByUserName', $user2);
     $um->setReturnReference('getUserByUserName', $user3);
     $current_changeset->removeRecipientsThatMayReceiveAnEmptyNotification($recipients);
     $this->assertEqual($recipients, array());
 }
    function testNotify()
    {
        $defs = array(array('has_changed' => 1, 'isNotificationsSupported' => 1, 'hasNotifications' => 0, 'recipients' => array()), array('has_changed' => 0, 'isNotificationsSupported' => 1, 'hasNotifications' => 1, 'recipients' => array('a_user')), array('has_changed' => 0, 'isNotificationsSupported' => 1, 'hasNotifications' => 0, 'recipients' => array('should_not_appear')), array('has_changed' => 0, 'isNotificationsSupported' => 0, 'hasNotifications' => 1, 'recipients' => array('should_not_appear_(not_supported)')), array('has_changed' => 0, 'isNotificationsSupported' => 1, 'hasNotifications' => 1, 'recipients' => array('multiple_users', '*****@*****.**')));
        $fact = new MockTracker_FormElementFactory();
        $dar = new MockDataAccessResult();
        $dao = new MockTracker_Artifact_Changeset_ValueDao();
        $artifact = new MockTracker_Artifact();
        $tracker = new MockTracker();
        $um = new MockUserManager();
        $i = 0;
        // try DRY in unit tests also... build mocks automatically
        foreach ($defs as $d) {
            $id = $i + 1;
            $changeset_value_id = 1000 + $id;
            $dar->setReturnValueAt($i++, 'current', array('changeset_id' => 66, 'field_id' => $id, 'id' => $changeset_value_id, 'has_changed' => $d['has_changed']));
            $f = new MockTracker_FormElement_Field_Selectbox();
            $fact->setReturnReference('getFieldById', $f, array($id));
            $f->setReturnValue('getId', $id);
            $f->setReturnValue('getLabel', 'field_' . $id);
            $f->setReturnValue('isNotificationsSupported', $d['isNotificationsSupported']);
            $f->setReturnValue('hasNotifications', $d['hasNotifications']);
            $f->setReturnValue('getRecipients', $d['recipients']);
            $p = new MockTracker_Artifact_ChangesetValue_List();
            $c = new MockTracker_Artifact_ChangesetValue_List();
            $c->setReturnValue('hasChanged', $d['has_changed']);
            $c->setReturnValue('diff', 'has changed', array($p));
            $f->setReturnReference('getChangesetValue', $c, array('*', $changeset_value_id, $d['has_changed']));
            unset($f);
        }
        $dar->setReturnValue('valid', true);
        $dar->setReturnValueAt($i, 'valid', false);
        $dao->setReturnReference('searchById', $dar);
        $artifact->setReturnReference('getTracker', $tracker);
        $artifact->setReturnValue('getCommentators', array('comment1', 'comment2'));
        $artifact->setReturnValue('getId', 666);
        $tracker->setReturnValue('getItemName', 'story');
        $tracker->setReturnValue('isNotificationStopped', false);
        $tracker->setReturnValue('getRecipients', array(array('recipients' => array('global1', 'global2'), 'on_updates' => false, 'check_permissions' => true), array('recipients' => array('dont_check_perms', 'global3', '*****@*****.**'), 'on_updates' => true, 'check_permissions' => false)));
        $current_changeset = new Tracker_Artifact_ChangesetTestVersion();
        $current_changeset->setReturnValue('getId', 66);
        $current_changeset->setReturnReference('getValueDao', $dao);
        $current_changeset->setReturnReference('getFormElementFactory', $fact);
        $current_changeset->setReturnReference('getArtifact', $artifact);
        $current_changeset->setReturnReference('getUserManager', $um);
        $current_changeset->setReturnReference('getTracker', $tracker);
        $expected_body = <<<BODY
story #666
<http://{$GLOBALS['sys_default_domain']}/tracker/?aid=666>

BODY;
        $current_changeset->expect('sendNotification', array(array('a_user', 'multiple_users', '*****@*****.**', 'dont_check_perms', 'global3', 'comment1', 'comment2'), array(), '[story #666]', $expected_body));
        $current_changeset->notify();
    }